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/ –