我试图通过 Gmail SMTP 服务器使用 JavaMail 发送一封电子邮件,代码如下:
final String username = "mygmail@gmail.com";
final String password = "mygmailpassword";
Properties props = new Properties();
props.put("mail.smtp.auth", true);
props.put("mail.smtp.starttls.enable", true);
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}
);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("no-reply@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("test@gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
返回一个 this 错误:
无法将套接字转换为 TLS;
完整的堆栈跟踪:
线程“ main”java.lang 中的异常。 RuntimeException: javax.mail。 MessagingException: 无法将套接字转换为 TLS; 嵌套的例外是: 异常: sun.security.validator。验证器异常: PKIX 路径构建失败: sun.security.Provider.certpath。 SunCertPathBuilderException: 无法找到到所请求目标的有效认证路径 At Test.main (Test.java: 43) 引起原因: javax.mail.MessagingException: 无法将套接字转换为 TLS; 嵌套的例外是: 异常: sun.security.validator。验证器异常: PKIX 路径构建失败: sun.security.Provider.certpath。 SunCertPathBuilderException: 无法找到到所请求目标的有效认证路径 Http://com.sun.mail.smtp.SMTPTransport. startTLS (SMTPTransport. java: 1907) 您可以访问 com.sun.mail.smtp.SMTPTransportation. ProtocolConnect (SMTPTransport. java: 666) 在 javax.mail. Service.connect (Service.java: 317) 在 javax.mail. Service.connect (Service.java: 176) 在 javax.mail. Service.connect (Service.java: 125) Http://javax.mail. Transport. send0(Transport. java: 194) 在 javax.mail. Transport. send (Transportation t.java: 124) At Test.main (Test.java: 38) 由 javax.net.ssl.SSLHandshakeException: sun.security.validator 引起。 ValidatorException: PKIX 路径构建失败: sun.security.prover.certpath。 SunCertPathBuilderException: 无法找到到所请求目标的有效证书路径 在 com.sun.net.ssl.interal.ssl. Alerts.getSSLException (Alerts.java: 174) 在 com.sun.net.ssl.interal.ssl.SSLSocketImp.death (SSLSocketImp.java: 1649) 地址: com.sun.net.ssl.interal.ssl. Handshaker.fatalSE (Handshaker.java: 241) 地址: com.sun.net.ssl.interal.ssl. Handshaker.fatalSE (Handshaker.java: 235) 地址: com.sun.net.ssl.interal.ssl. ClientHandshaker.serverSecurities (ClientHandshaker.java: 1206) 在 com.sun.net.ssl.interal.ssl. ClientHandshaker.processMessage (ClientHandshaker.java: 136) 在 com.sun.net.ssl.interal.ssl. Handshaker.processLoop (Handshaker.java: 593) 地址: com.sun.net.ssl.interal.ssl. Handshaker.process _ record (Handshaker.java: 529) 请访问 com.sun.net.ssl.interal.ssl.SSLSocketImp.readRecord (SSLSocketImp.java: 893) 请访问 com.sun.net.ssl.interal.ssl.SSLSocketImp.PerformInitialHandshake (SSLSocketImp.java: 1138) 地址: com.sun.net.ssl.interal.ssl.SSLSocketImp.startHandshake (SSLSocketImp.java: 1165) 地址: com.sun.net.ssl.interal.ssl.SSLSocketImp.startHandshake (SSLSocketImp.java: 1149) 在 com.sun.mail.util. SocketFetcher.configureSSLSocket (SocketFetcher.java: 549) 在 com.sun.mail.util. SocketFetcher.startTLS (SocketFetcher.java: 486) Http://com.sun.mail.smtp.SMTPTransportation. startTLS (SMTPTransport. java: 1902) 还有七个 由: sun.security.validator 引起。 ValidatorException: PKIX 路径构建失败: sun.security.prover.certpath。 SunCertPathBuilderException: 无法找到到所请求目标的有效证书路径 请访问 sun.security.validator.PKIXValidator.doBuild (PKIXValidator.java: 323) Http://sun.security.validator.PKIXValidator.engineering 验证(PKIXValidator.java: 217) 在 sun.security.validator. Validator.valid(Validator.java: 218) 在 com.sun.net.ssl.interal.ssl.X509TrustManagerImp.valid(X509TrustManagerImp.java: 126) X509TrustManagerImp.checkServerTrusted (X509TrustManagerImp.java: 209) X509TrustManagerImp.checkServerTrusted (X509TrustManagerImp.java: 249) 地址: com.sun.net.ssl.interal.ssl. ClientHandshaker.serverSecurities (ClientHandshaker.java: 1185) 还有17个 原因是: sun.security.provision. certpath. SunCertPathBuilderException: 无法找到到所请求目标的有效证书路径 Http://sun.security.provision. certpath. SunCertPathBuilder.engineering Build (SunCertPathBuilder.java: 174) Cert. CertPathBuilder.build (CertPathBuilder.java: 238) 请访问 sun.security.validator.PKIXValidator.doBuild (PKIXValidator.java: 318) 还有23个