0

我在我的 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:连接超时:在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java) 处连接 com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) :699) 在 javax.mail.Service.connect(Service.java:388) 在 javax.mail.Service.connect(Service.java:246) 在 javax.mail.Service.connect(Service.java:195) 在 javax .mail.Transport.send0(Transport.java:254) 在 javax.mail.Transport.send(Transport.java:124) 在 ge.psda.camunda.sendMails.SendMails.send(SendMails.java:40) ...还有 208 个

PS我在我的邮件中启用了不太安全的功能,但这不能帮助我我应该改变什么才能发送邮件?

4

1 回答 1

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>
于 2018-03-07T09:08:15.147 回答