我在我的camunda环境中有这个类(我使用这个类就像在camunda模型器中的服务任务一样):
public class SendMails implements JavaDelegate{
String text,from,password;
Object jsonObject1=new JSONObject();
public static void send( final String from, final String password,String to,String sub,String msg){
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
public void execute(DelegateExecution execution) throws Exception {
text = execution.getVariable("selectedDocuments").toString();
send("demo@camunda.org","xxxxx","test@gmail.com","hello javatpoint","How r u?");
}
}
这是我的POM:
<dependencies>
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-engine</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
但是当我尝试发送消息时,我遇到了这样的错误:
引起:com.sun.mail.util.MailConnectException:无法连接到 主持人,港口:smtp.gmail.com,465;超时-1;嵌套异常是: java.net.ConnectException:连接超时:connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) 在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699) 在javax.mail.Service.connect(Service.java:388)at javax.mail.Service.connect(Service.java:246)at javax.mail.Service.connect(Service.java:195)at javax.mail.Transport.send0(Transport.java:254)at javax.mail.Transport.send(Transport.java:124)at ge.psda.camunda.sendMails.SendMails.send(SendMails.java:40)... 208 更
P.S我已经在我的邮件中启用了较少的安全性,但是这无法帮助我改变什么才能发送邮件?
答案 0 :(得分:0)
您必须为发件人提供凭据。您已为gmail配置(属性)它。实际上,您已经为要发送电子邮件的smtp服务器配置了属性。
您需要的依赖项:
<dependencies>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
</dependencies>