使用javamail发送邮件会抛出NoSuchMethodError:sun.security.ssl.SessionId.checkLength

时间:2018-05-19 17:54:04

标签: ssl javamail nosuchmethoderror glassfish-5 java-ee-8

My Java ee 8 webapp正在使用javamail 1.6.1通过gmail发送电子邮件。我在Glassfish 5,Mac os High Sierra上运行它,Glassfish 5在jdk1.8.0_20上运行,我从Netbeans 8.2开始运行。

这是完成它的片段:

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);
}

Transport.send行会在上面的标题中引发异常。更确切地说:

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)

我发现了这个similar question here,但他们所说的只是为了确保Glassfish在JDK 1.8上运行,而我的是。{/ p>

我注意到两个dependecies,javaee-api和javamail都存在相同的javax.mail.Transport类,所以我从pom.xml中的依赖项中删除了javamail,我仍然可以构建应用程序,但是得到了同样的错误在尝试发送电子邮件时。

这就是我的pom.xml:

    <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 个答案:

答案 0 :(得分:0)

我将JDK 8升级到1.8.0_172并开始收到其他错误:

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

这显然与Glassfish 5的问题一样here

所以我将JDK 8版本降级为1.8.0_152,这解决了这个问题。

要设置netbeans以使用jdk的正确更新,请在文件netbeans_home/etc/netbeans.conf中配置它:

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

答案 1 :(得分:0)

不建议降级JDK。我找到了另一个对我有用的解决方案。请参阅sun.security.ssl.SSLSessionImpl not found中的Antoine答案。您可以使用7ZIP等zip实用程序从相关文件中删除sun。*软件包。

我尚未尝试处理此特定错误消息,但我怀疑这将是解决方案,因为似乎有许多与该软件包相关的类似问题具有相同的根本原因。