Javamail 无法将套接字转换为 TLS GMail

2022-08-31 13:24:11

我正在尝试通过 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);
}

返回以下错误:

无法将套接字转换为 TLS;

完整的堆栈跟踪:

线程“main” java.lang.RuntimeException: javax.mail.MessagingException: 无法将套接字转换为TLS;嵌套的异常是: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX 路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException: 无法在 Test.main(Test.java:43) 找到到请求目标的有效证书路径 导致: javax.mail.MessagingException: 无法将套接字转换为 TLS;嵌套的例外是: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: 无法在 com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666) at javax.mail.Service.connect(Service.java:317) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at Test.main(Test.java:38) 由: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.certpath.SunCertPathBuilderException: 無法找到有效驗證程到所請求的目標com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:174) at com.sun.net.ssl.internal.ssl.sslsocketImpl.fatal(SSLSocketImpl.java:1649) at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:241) at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Handshaker.java:235) at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1206) at com.sun.net.ssl.internal.ssl.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:136) at com.sun.net.ssl.internal.ssl.ssl.Handshaker.processLoop(Handshaker.java:593) at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Handshaker.java:529) at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:893) at com.sun.net.ssl.ssl.internal.ssl.sslsocketImpl.performInitialHandshake(SSLSocketImpl.java:1138) at com.sun.net.ssl.internal.sslsocketImpl.startHandshake(SSLSocketImpl.java:1165) atcom.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149) at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:549) at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:486) at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902) ...7 更多 原因: sun.security.validator.Validator.validator:PKIX 路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException: 无法在 sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:323) at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:217) at sun.security.validator.Validator.validator.validate(Validator.java:218) at com.sun.net.ssl.internal.ssl.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:126) at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:209) at com.sun.net.ssl.internal.ssl.X509TrustmanagerImpl.checkServerTrusted(X509TrustManagerImpl.java:249) at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1185) ...17 更多 原因: sun.security.provider.certpath.SunCertPathBuilderException: 无法在 sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:174) at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:238) at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:318) 找到请求目标的有效证书路径...另外 23


答案 1
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

答案 2

我禁用了avast防病毒软件10分钟,并让它工作。