【发布时间】:2017-12-24 19:21:57
【问题描述】:
我正在尝试使用 SMTP 发送邮件,它设置为具有 25,587 个端口和 TLS 的默认 smtp 服务器。 MS Exchange 2010。所有身份验证数据都是正确的,因为我可以通过邮件的 Web 界面登录。下面的代码确实适用于 gmail smtp 服务器,但是当我使用自己的 smtp 服务器时,它会抛出证书路径错误:
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();}
}
我做错了吗?
【问题讨论】: