Can't send java mail to smtp host

I have this class inside my camunda environment(and i use this class like as service task inside camunda modeller):
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?");  

        }
    }

HERE IS MY 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>

but when i try to send message i got errors like this:

Caused by: com.sun.mail.util.MailConnectException: Couldn’t connect to host, port: smtp.gmail.com, 465; timeout -1; nested exception is: java.net.ConnectException: Connection timed out: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699) at 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 more

P.S i have enabled less secure up in my mail but this can’t helped me what should i change to be able to send mail?

Hi,

Try with mail.sender set to the email account name rather than Camunda.

In addition;
Make sure your mail config is loaded by the app server, eg how do you install it.
Makes sure the jar files in the classpath are compatible with whats specified, eg mail, activation etc.
Also ensure there are no earlier versions of the library jars in places like the app server library folder…

regards

Rob

hi @Webcyberrob
thank you for your response, i have changed my question content and logic in order to make it more clear, do you have any idea what should be the reason of connection errors?
Thank you in advance :smiley:

Hi,

Do you have a firewall or similar either blocking the outbound connection or blocking the inbound response?

regards

Rob

Yes i have firewall , is there any ability to configure this procedure without firewall?

Hi,

Try adding this line to your props as the first line:
props.put(“mail.smtp.starttls.enable”, “true”);

-Jarkko

Hi,
@JARKKO_PARVIAINEN thank you for your response i have added this props.put(“mail.smtp.starttls.enable”, “true”); to my code, when i run this peace of code like java application apart from camunda environment i can send mail but in camunda environment i am stacukec at this point i mean neither i can send mail and nor get any exception