String to="[email protected]";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session sess = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("[email protected]","password");//change accordingly
}
});
//compose message
try {
MimeMessage message = new MimeMessage(sess);
message.setFrom(new InternetAddress("[email protected]"));//change accordingly
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello");
message.setText("Testing.......");
//send message
Transport.send(message);
System.out.println("message sent successfully");
}
catch (MessagingException e) {
out.println(e);}
在发送邮件Java的SSL:无法找到请求的目标
javax.mail.MessagingException的我收到以下错误有效证书路径:异常读数响应;嵌套的例外是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径建设失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求的目标的有效证书路径
添加属性'props.put(“mail.pop3s.ssl.trust”,“*”);'并检查。 – NINCOMPOOP
不,我仍然得到相同的异常 –
如果你用google搜索错误信息,你会发现几十个(数百)命中。您必须将相应的CA证书添加到Java密钥库。请参阅http://www.mikepilat.com/blog/2011/05/adding-a-certificate-authority-to-the-java-runtime/ –