我之前在几个项目中使用过javamail,它一直运行良好,直到最近,(可能与升级到java 8有什么关系?),现在它总是返回以下异常:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
javax.net.ssl.SSLKeyException: RSA premaster secret error
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1963)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:367)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at one.Demo.main(Demo.java:39)
Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error
at sun.security.ssl.RSAClientKeyExchange.<init>(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:528)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:333)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
... 7 more
Caused by: java.security.NoSuchAlgorithmException: SunTlsRsaPremasterSecret KeyGenerator not available
at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:159)
at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:208)
at sun.security.ssl.JsseJce.getKeyGenerator(Unknown Source)
... 20 more
从我的构造来看,似乎错误来自传输类的send方法。这是我遇到的问题的MCVE。它使用Gmail stmp服务器。
import java.util.Date;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class Demo {
public static void main(String[] args) throws MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", 465);
props.put("mail.smtp.ssl.enable", true);
props.put("mail.smtp.auth", true);
Authenticator auth = new Authenticator() {
private PasswordAuthentication pa = new PasswordAuthentication(
"my_email",
"my_password");
public PasswordAuthentication getPasswordAuthentication(){
return pa;
}
};
Session session = Session.getInstance(props, auth);
session.setDebug(false);
MimeMessage message = new MimeMessage(session);
Address address = new InternetAddress("other_email");
message.addRecipient(Message.RecipientType.TO, address);
message.setFrom(new InternetAddress("my_email"));
message.setSubject("Test");
message.setText("This is a test of the Javamail API.");
message.setSentDate(new Date());
Transport.send(message);
}
}