javaemailexceptionconnectexception

exception while sending an email in java using Appache Commons email library


org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.office365.com:465
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1469)
    at org.apache.commons.mail.Email.send(Email.java:1496)
    at com.raghedabusaad.SimpleEmailExample.main(SimpleEmailExample.java:30)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.office365.com, 465; timeout 60000;
  nested exception is:
    java.net.ConnectException: Connection timed out: no further information
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2209)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:740)
    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 org.apache.commons.mail.Email.sendMimeMessage(Email.java:1459)
    ... 2 more
Caused by: java.net.ConnectException: Connection timed out: no further information
    at java.base/sun.nio.ch.Net.pollConnect(Native Method)
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672)
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:554)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:602)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)
    at java.base/java.net.Socket.connect(Socket.java:633)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:357)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:217)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2175)
    ... 9 more

what are possible reasons to encounter such exception? this is the code used:(address and password were input correctly). I also tried turning of the firewall and antivirus but still got the same exception. (even with gmail host name)

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

public class SimpleEmailExample {
    public static void main(String[] args) {
        try {
            Email email = new SimpleEmail();
            
            email.setHostName("smtp.office365.com"); 
            email.setSmtpPort(587); 
            email.setAuthenticator(new DefaultAuthenticator("address", "password"));
            email.setSSLOnConnect(true);
            email.setFrom("address"); 
            email.setSubject("appache commons trial");
            email.setMsg("Appache commons trial successful");
            email.addTo("address"); 
            email.send();

            System.out.println("Simple email sent successfully.");
        } catch (EmailException e) {
            e.printStackTrace();
---

        }
    }
}

Solution

  • Use the below code where I have made 1 modification. I have replaced email.setSSLOnConnect(true); with email.setStartTLSEnabled(true);

    import org.apache.commons.mail.DefaultAuthenticator;
    import org.apache.commons.mail.Email;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.SimpleEmail;
    
    public class EmailDemo {
        public static void main(String[] args) {
            try {
                Email email = new SimpleEmail();
                
                String address = "abc@outlook.com";
                email.setHostName("smtp.office365.com"); 
                email.setSmtpPort(587); 
                email.setAuthenticator(new DefaultAuthenticator(address, "password"));
                email.setStartTLSEnabled(true);
                email.setFrom(address); 
                email.setSubject("appache commons trial");
                email.setMsg("Appache commons trial successful");
                email.addTo(address); 
                email.send();
    
                System.out.println("Simple email sent successfully.");
            } catch (EmailException e) {
                e.printStackTrace();
    
    
            }
        }
    }
    

    I had to switch the method from SSL to StartTLS because the 587 port works with StartTLS. Additionally, for authentication purposes, make sure not to enable two-factor authentication; otherwise, you will encounter authentication errors

    Port 587 and 465 are both frequently used for SMTPS traffic. Port 587 is often used to encrypt SMTP messages using STARTTLS, which allows the email client to establish secure connections by requesting that the mail server upgrade the connection through TLS.