Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java mail cannot connect to smtp using tls or ssl

0

I am trying to connect to a mail server from Java. I have been able to successfully connect to many mail servers from Java using the same code, including Gmail, Rackspace, GoDaddy, and others, but this one does not work no matter the settings I try.

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", this.outgoingHost);
    props.put("mail.smtp.port", 587);
    props.put("mail.smtp.ssl.trust", this.outgoingHost);
    session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

This fails with, javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

I also tried,

    props.put("mail.smtp.host", this.outgoingHost);
    props.put("mail.smtp.port", 587);
    props.put("mail.smtp.socketFactory.port", 587);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.ssl.trust", this.outgoingHost);
    session = Session.getInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

This fails with, javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 550 SMTP AUTH is required for message submission on port 587

I also tried port 465, but it gets a timeout.

I was able to send mail using the same smtp settings from Thunderbird email client (same host/port/user/pw, but it must be doing something else).

Using Java Mail 1.51

Any ideas?

like image 404
James Avatar asked Dec 07 '22 10:12

James


1 Answers

When you use SSL (smtps), you don't use STARTTLS (msa) and vice versa. SSL defaults to port 465 and TLS to port 587. You might also have to set the SSL protocols using mail.smtp.ssl.protocols or mail.smtps.ssl.protocols to specify the SSL protocols that will be enabled for SSL/TLS connections. You better avoid overriding PasswordAuthentication for sending credentials and use SMTPTransport connect method. Also it's very important that for SSL, you have to use smtps for mail.transport.protocol and use mail.smtps props instead of mail.smtp. I'll provide examples for both SSL and TLS.

You can debug and see the whole communication in the console using session.setDebug(true) or props.put("mail.debug", "true"); that will help a lot as you'll see the whole telnet communication with the server.

Using TLS (STARTTLS) 587:

// Set debug so we see the whole communication with the server
props.put("mail.debug", "true");

props.put("mail.transport.protocol", "smtp");
props.put("mail.host", outgoingHost);
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");

// Enable STARTTLS
props.put("mail.smtp.starttls.enable", "true");

// Accept only TLS 1.1 and 1.2
props.setProperty("mail.smtp.ssl.protocols", "TLSv1.1 TLSv1.2");

Session session = Session.getInstance(props, null);
session.setDebug(true);

// Create an SMTP transport from the session
SMTPTransport t = (SMTPTransport)session.getTransport("smtp");

// Connect to the server using credentials
t.connect(outgoingHost, username, password);

// Send the message
t.sendMessage(msg, msg.getAllRecipients());

Sample debug output using STARTTLS:

Notice the TRANSPORT protocol is smtp

DEBUG: JavaMail version 1.5.1
...
DEBUG: setDebug: JavaMail version 1.5.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.example.com", port 587, isSSL false
220-srv.example.com ESMTP Exim 4.92 #2 Wed, 18 Mar 2020 15:55:56 +0200 
220-We do not authorize the use of this system to transport unsolicited, 
220 and/or bulk e-mail.
DEBUG SMTP: connected to host "mail.example.com", port: 587

EHLO host.docker.internal
250-srv.example.com Hello ppp.home.provider.com [x.x.x.x]
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250-STARTTLS
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "HELP", arg ""
STARTTLS
220 TLS go ahead
EHLO host.docker.internal
250-srv.example.com Hello ppp.home.provider.com [x.x.x.x]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 OK
RCPT TO:<[email protected]>
250 Accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354 Enter message, ending with "." on a line by itself
Date: Wed, 18 Mar 2020 15:55:57 +0200 (EET)
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Test from JAVA!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: smtpsend

Message from the JAVA app STARTTLS!
.
250 OK id=1jEZAt-009Y7x-5Z
Response: 250 OK id=1jEZAt-009Y7x-5Z

QUIT
221 srv.example.com closing connection

Using SSL 465:

// Set debug so we see the whole communication with the server
props.put("mail.debug", "true");

// All mail props for protocol will be mail.smtps

// We set smtps transport protocol for SSL
props.put("mail.transport.protocol", "smtps");
props.put("mail.host", outgoingHost);
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.port", "465");
props.put("mail.smtps.ssl.trust", outgoingHost);
props.put("mail.smtps.ssl.enable", "true");

// Accept only TLS 1.1 and 1.2
props.setProperty("mail.smtps.ssl.protocols", "TLSv1.1 TLSv1.2");

Session session = Session.getInstance(props, null);
session.setDebug(true);

// Create an SMTP transport from the session
SMTPTransport t = (SMTPTransport)session.getTransport("smtps");

// Connect to the server using credentials
t.connect(outgoingHost, username, password);

// Send the message
t.sendMessage(msg, msg.getAllRecipients());

Sample debug output using SSL:

Notice the TRANSPORT protocol is smtps

DEBUG: JavaMail version 1.5.1
...
DEBUG: setDebug: JavaMail version 1.5.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "mail.example.com", port 465, isSSL true
220-srv.example.com ESMTP Exim 4.92 #2 Wed, 18 Mar 2020 16:09:51 +0200 
220-We do not authorize the use of this system to transport unsolicited, 
220 and/or bulk e-mail.
DEBUG SMTP: connected to host "mail.example.com", port: 465

EHLO host.docker.internal
250-srv.example.com Hello ppp.home.provider.com [x.x.x.x]
250-SIZE 52428800
250-8BITMIME
250-PIPELINING
250-AUTH PLAIN LOGIN
250 HELP
DEBUG SMTP: Found extension "SIZE", arg "52428800"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: Found extension "HELP", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<[email protected]>
250 OK
RCPT TO:<[email protected]>
250 Accepted
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   [email protected]
DATA
354 Enter message, ending with "." on a line by itself
Date: Wed, 18 Mar 2020 16:09:50 +0200 (EET)
From: [email protected]
To: [email protected]
Message-ID: <[email protected]>
Subject: Test from JAVA!
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
X-Mailer: smtpsend

Message from the JAVA app SSL!
.
250 OK id=1jEZOK-009bbA-5C
Response: 250 OK id=1jEZOK-009bbA-5C

QUIT
221 srv.example.com closing connection
like image 148
Christos Lytras Avatar answered Dec 25 '22 22:12

Christos Lytras