[英]sending mail with javamail throws NoSuchMethodError: sun.security.ssl.SessionId.checkLength
My Java ee 8 webapp is using javamail 1.6.1 to send emails via gmail. 我的Java ee 8 Web应用程序正在使用javamail 1.6.1通过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.
我在Glassfish 5,Mac os High Sierra上运行它,并且Glassfish 5设置为在jdk1.8.0_20上运行,并且我从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. Transport.send
行在上面的标题中引发异常。 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. 我在这里找到了类似的问题,但他们所说的只是确保Glassfish在JDK 1.8上运行,而我的是。
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. 我注意到同一个
javax.mail.Transport
类在依赖关系(javaee-api和javamail)中都存在,因此我从pom.xml的依赖项中删除了javamail,我仍然可以构建该应用程序,但是在尝试执行该操作时遇到了相同的错误发送电子邮件。
This is what my pom.xml looks like: 这是我的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>
I upgraded the JDK 8 to 1.8.0_172 and started getting this other error: 我将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
That apparently is an issue with Glassfish 5 as discussed here . 显然,这是此处讨论的Glassfish 5的问题。
So I downgraded the JDK 8 version to 1.8.0_152, which solved this problem. 因此,我将JDK 8版本降级为1.8.0_152,从而解决了此问题。
To set netbeans to use the right update of the jdk configure this in the file netbeans_home/etc/netbeans.conf
: 要将netbeans设置为使用jdk的正确更新,请在文件
netbeans_home/etc/netbeans.conf
:
netbeans_jdkhome=/Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home
Downgrading your JDK is not advisable. 不建议降级JDK。 I found another solution which worked for me.
我找到了另一个对我有用的解决方案。 See Antoine's answer in sun.security.ssl.SSLSessionImpl not found .
请参见未找到sun.security.ssl.SSLSessionImpl中的 Antoine答案。 You can remove the sun.* packages from the relevant file using a zip utility such as 7ZIP.
您可以使用zip实用程序(例如7ZIP)从相关文件中删除sun。*软件包。
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. 我没有针对此特定错误消息进行尝试,但我怀疑这将是解决方案,因为似乎有许多与该软件包相关的类似问题具有相同的根本原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.