我的 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 上运行,而我的是。
我注意到javax.mail.Transport
在 javaee-api 和 javamail 这两个依赖项中都存在同一个类,所以我从我的 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>