TLS SMTP邮件Java引发SSLHandShakeException
问题描述:
我试图发送邮件使用SMTP,它设置为具有25,587个端口和TLS的默认SMTP服务器。 MS Exchange 2010.所有身份验证数据都是正确的,因为我可以通过邮件的Web界面登录。下面的代码不使用Gmail的SMTP服务器工作,但是当我用我自己的SMTP服务器,它抛出我的证书路径错误:TLS SMTP邮件Java引发SSLHandShakeException
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:PKIX路径建筑失败: sun.security.provider.certpath.SunCertPathBuilderException:无法找到 有效证明路径请求的目标
public static void SendMailTLS(String to, String subject, String body) throws Exception{
try {
java.util.Properties props = new java.util.Properties();
props.put("mail.smtp.host", "sample");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.connectiontimeout", "10000");
final String EmailUser = "sample";
final String EmailPassword = "sample";
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
EmailUser,EmailPassword);
}
}); session.setDebug(true);
InternetAddress fromAddress = new InternetAddress("sample",
"sample");
InternetAddress toAddress = new InternetAddress("sample",
"sample");
Message msg = new MimeMessage(session);
msg.setFrom(fromAddress);
msg.addRecipient(Message.RecipientType.TO,toAddress);
msg.setSubject(subject);
msg.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect();
transport.sendMessage(msg, msg.getAllRecipients());
} catch (MessagingException e) {e.printStackTrace();}
}
难道我做错了什么?
答
此错误消息意味着名为“sample”(MS Exchange 2010服务器)的服务器正在使用您的Java运行时不受信任的证书。您可能正在使用自签名证书。所以您需要使用keytool将此证书导入Java运行时证书存储区。