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



我的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)

我在这里发现了类似的问题,但他们所说的只是确保 Glassfish 在 JDK 1.8 上运行,而我的是。

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

这是我的绒球.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>

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

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

我将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的问题。

所以我将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

最新更新