Unrecognized SSL message while sending mail from localhost using STARTTLS

Sumit picture Sumit · Sep 25, 2014 · Viewed 7.2k times · Source

I am currently investigating James Mail Server and have an instance running on my local machine. I am trying to send an email using the below code by am running into the exception listed further down:

public static void send() {
    try {
        String host = "127.0.0.1";

        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtps.port", "465");
        props.put("mail.smtps.ssl.trust", host);

        Session mailSession = Session.getInstance(props);
        mailSession.setDebug(true);

        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(frAddress));
        InternetAddress[] address = { new InternetAddress(toAddress) };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(message);

        Transport transport = mailSession.getTransport("smtps");
        transport.connect(host, userName, password);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
}

The below exception is encountered at transport.connect(host, userName, password)

DEBUG: setDebug: JavaMail version 1.4.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "127.0.0.1", port 465, isSSL true
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
javax.mail.MessagingException: Exception reading response;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1764)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1523)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at com.spectramd.securemail.bouncycastle.JavaMailTest.send(JavaMailTest.java:50)
    at com.spectramd.securemail.bouncycastle.JavaMailTest.main(JavaMailTest.java:24)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.AppInputStream.read(Unknown Source)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:106)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:84)
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1742)
    ... 6 more

I have followed the steps listed here to configure the James server to start on SSL and also added a dummy JKS certificate as per the steps. I been looking online but so far, I am not able to find a solution. Plz help.

EDIT:

Forgot to mention, I am successfully able to send mails using plain SMTP with the default configurations James is shipped with.

Answer

Bill Shannon picture Bill Shannon · Sep 30, 2014

Well, it looks like James isn't configured properly. If you use telnet to connect to it on port 465, do you get a plain text response?

Also, you're using a pretty old version of JavaMail. You might want to upgrade to the latest version.