我尝试使用下一个代码发送电子邮件:
...
Security.addProvider(new BouncyCastleProvider());
...
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
...
Transport t = session.getTransport("smtp");
try {
t.connect();
t.sendMessage(message, message.getAllRecipients());
System.out.println("Done");
} finally {
t.close();
}
...
但它引发了下一个异常:
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
java.net.SocketException: java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: com.sun.net.ssl.internal.ssl.DefaultSSLContextImpl)
如果我不添加提供程序,它可以正常工作,但我需要在我的代码中解决其他问题。
//Security.addProvider(new BouncyCastleProvider());