sending mail with javamail throws NoSuchMethodError: sun.security.ssl.SessionId.checkLength

1.4k Views Asked by At

My Java ee 8 webapp is using javamail 1.6.1 to send emails via gmail. I'm running it on Glassfish 5, Mac os High Sierra, and Glassfish 5 is set to run on jdk1.8.0_20 and I start it from Netbeans 8.2.

Here's the snippet where it's being done:

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);

// Get the Session object.
Session session = Session.getInstance(props,
    new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

try {
    Message message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
    message.setSubject("Testing Subject");
    message.setContent(
                "<h1>This message is embedded in HTML tags</h1>",
                "text/html");
    Transport.send(message); // this line throws the exception
} catch (MessagingException e) {
    throw new RuntimeException(e);
}

The Transport.send line throws the exception in the title above. More precisely:

Caused by: java.lang.NoSuchMethodError: sun.security.ssl.SessionId.checkLength(Lsun/security/ssl/ProtocolVersion;)V
    at sun.security.ssl.HandshakeMessage$ServerHello.<init>(HandshakeMessage.java:504)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:984)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:919)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1043)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1343)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1371)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1355)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:619)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:546)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:2135)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:738)
    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)

I've found this similar question here but all they say is to make sure that Glassfish is running on JDK 1.8, and mine is.

I noticed that the same javax.mail.Transport class exists in both dependecies, javaee-api and javamail, so I removed javamail from my dependencies in my pom.xml and I could still build the application, but got the same error when trying to send the email.

This is what my pom.xml looks like:

    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.6.1</version>
    </dependency>
2

There are 2 best solutions below

0
On

Downgrading your JDK is not advisable. I found another solution which worked for me. See Antoine's answer in sun.security.ssl.SSLSessionImpl not found . You can remove the sun.* packages from the relevant file using a zip utility such as 7ZIP.

I haven't tried for this particular error message but I suspect it will be the solution as there seems to be many similar questions related to this package with the same root cause.

1
On

I upgraded the JDK 8 to 1.8.0_172 and started getting this other error:

NoSuchMethodError: sun.security.ssl.SSLSessionImpl.<init>(Lsun/security/ssl/ProtocolVersion;Lsun/security/ssl/CipherSuite;Ljava/util/Collection;Lsun/security/ssl/SessionId;Ljava/lang/String;I)V

That apparently is an issue with Glassfish 5 as discussed here.

So I downgraded the JDK 8 version to 1.8.0_152, which solved this problem.

To set netbeans to use the right update of the jdk configure this in the file netbeans_home/etc/netbeans.conf :

netbeans_jdkhome=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home