Lucas
Lucas

Reputation: 43

Error sending email with Java Commons Mail

Good morning! I have searched a lot here to find a solution, but everything I found didn't work. I have the class Mail:

package model.mail;

import java.nio.charset.Charset;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class Mail extends MultiPartEmail {

    // Default constructor
    public Mail() throws EmailException {

        this( null, null, null, null);

    }

    // My Constructor
    @SuppressWarnings("deprecation")
    public Mail( String receiver, String subject, String message, EmailAttachment[] attachments ) throws EmailException {

        this.addTo( receiver, "" );

        this.setSubject( new String( subject.getBytes( Charset.forName("utf-8") ), Charset.forName("utf-8") ) );

        this.setMsg( new String( message.getBytes( Charset.forName("utf-8") ), Charset.forName("utf-8") ) );

        if( attachments != null ) {
            for ( int i = 0; i < attachments.length; i++ )
                this.attach( attachments[i] );
        }

        this.setSSL(true);

    }

}

And the Main class:

package main;

import model.mail.Mail;

public class Main {

    public static void main(String[] args) {

        try {

            Mail email = new Mail( "[email protected]", "Teste", "teste..", null );
            email.setHostName( "smtp.mail.yahoo.com" );
            email.setSmtpPort( 587 );
            email.setAuthentication( "[email protected]", "mypassword" );
            email.setFrom( "[email protected]", "My Name" );
            email.send();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

This code used to work, but it's not working anymore. When I run it, I get this exception:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.mail.yahoo.com:465
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
    at org.apache.commons.mail.Email.send(Email.java:1437)
    at main.Main.main(Main.java:29)
Caused by: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.mail.yahoo.com, 465; timeout 60000;
  nested exception is:
    java.net.ConnectException: Connection timed out: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1961)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    at javax.mail.Service.connect(Service.java:367)
    at javax.mail.Service.connect(Service.java:226)
    at javax.mail.Service.connect(Service.java:175)
    at javax.mail.Transport.send0(Transport.java:253)
    at javax.mail.Transport.send(Transport.java:124)
    at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
    ... 2 more
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:295)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:208)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 9 more

Please, forgive-me if I did some mistakes, I am new here.

UPDATED: I finally found the error! Probably, the mail server changed and now it requires TLS authentication. I changed the authentication to TLS and it worked!

Upvotes: 1

Views: 4925

Answers (1)

Fabio
Fabio

Reputation: 116

You are setting port 587, but then in the method is enabled SSL(465)

this.setSSL(true);

Your firewall may be blocking port 465. Try to remove that piece of code and use for real port 587 and see if it works. If so, you have to contact your network administrator or struggle with network configuration.

Upvotes: 1

Related Questions