0
我已經在多個項目中使用的JavaMail之前,它一直運作良好,直到最近,(也許一些與升級做java的8?),當它現在總是返回以下異常:JavaMail的傳輸錯誤
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);
}
}
也許這有助於:http://stackoverflow.com/questions/2872520/suntlsrsapremastersecret-keygenerator-not-available – salyh