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

时间:2013-04-20 01:00:30

标签: java email gmail javamail

我正在尝试通过gmails SMTP Server使用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);
    }

返回此错误

Could not convert socket to TLS;

完整的堆栈跟踪

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at Test.main(Test.java:43)
Caused by: javax.mail.MessagingException: Could not convert socket to TLS;
  nested exception is:
    javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at 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)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at 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.ClientHandshaker.processMessage(ClientHandshaker.java:136)
    at com.sun.net.ssl.internal.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.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)
    at com.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 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:323)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:217)
    at sun.security.validator.Validator.validate(Validator.java:218)
    at com.sun.net.ssl.internal.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 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at 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 more

16 个答案:

答案 0 :(得分:111)

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

答案 1 :(得分:20)

确保您的防病毒程序没有干扰,并确保在防火墙中添加排除项。

答案 2 :(得分:19)

我禁用avast防病毒10分钟并使其正常工作。

答案 3 :(得分:10)

当用于发送电子邮件的默认 SSL 协议设置为 TLSv1 并且 smtp 服务器不再支持此协议时,我遇到了类似的问题。 诀窍是使用较新的协议:

mail.smtp.ssl.protocols=TLSv1.2

答案 4 :(得分:7)

@carlos 的第一个答案为我效劳:

session.getProperties().put("mail.smtp.ssl.trust", "smtp.gmail.com");

我已经测试了下面的属性,并且对我来说也很完美:

session.getProperties().put("mail.smtp.starttls.enable", "true");

这两个属性单独解决了这类问题,但保证我同时使用了这两个问题。

答案 5 :(得分:6)

经过一整天的搜索,我禁用了Avast 10分钟并禁用了Windows防火墙(重要),一切开始正常工作!

这是我的错误:

Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target. Failed messages: javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target  

以下是通过在SMTP端口587(或应用程序使用的任何端口)中添加排除项来解决Avast 19.8.2393中的问题的方法:

  1. 打开Avast

  2. 点击“设置”

  3. 单击“疑难解答”,然后单击“打开旧设置”

enter image description here

  1. 再次单击“疑难解答”,向下滚动到“重定向设置”,然后删除您的应用使用的端口。

enter image description here

就我而言,我刚刚从SMTP端口中删除了587。

现在,我可以使用Avast并已打开Windows防火墙(无需为防火墙添加其他排除项)。

这是我的 application.properties 电子邮件属性:

###### I am using a Google App Password which I generated in my Gmail Security settings ######
spring.mail.host = smtp.gmail.com
spring.mail.port = 587
spring.mail.protocol = smtp
spring.mail.username = gmail account
spring.mail.password = password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000

答案 6 :(得分:2)

对于我来说,问题是通过删除

解决的
prop.put("mail.smtp.starttls.enable", "true");

这可能是由于电子邮件服务器上的某些ssl配置错误引起的,我不确定。电子邮件服务器管理员从不接受它,而总是将责任归咎于托管服务提供商:)

答案 7 :(得分:1)

如果你的上下文是android应用程序, 然后确保您的Android设备时间设置为当前日期和时间。 潜在的例外是 “SSL证书未经过身份验证。”

答案 8 :(得分:1)

是的,它在本地主机上对我有用:

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

错误只在本地主机上抛出,所以你也应该在远程服务器上尝试。通常,上面提到的代码片段在没有 ssl.trust 属性

的情况下运行良好

答案 9 :(得分:0)

尝试使用JavaMail附带的smtpsend程序,如here所述。如果以同样的方式失败,那么您的JDK配置或网络配置就会出现问题。

答案 10 :(得分:0)

帮助我解决这个问题的原因是,在此之前我已经尝试了一切,就是将我安装的jre配置为JRE 1.8。

Eclipse中的步骤:Windows>首选项> java>已安装JRE> jre1.8.0

如果设置为jdk,请切换到jre(默认情况下应该使用最新的java版本设置)。

答案 11 :(得分:0)

尝试将端口更改为465

mail.smtp.socketFactory.port=465
mail.smtp.port=465

答案 12 :(得分:0)

如果应用程序不支持SMTP主机使用的 TLS版本,则也可能是这样。

例如,当您的应用程序(或使用较旧javax.mail JAR的Java程序)仅支持TLSv1.1时,尝试配置不使用回退功能的TLSv1.2的SMTP服务器。

答案 13 :(得分:0)

在application.properties之上对我来说很棒:

spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com

答案 14 :(得分:0)

检查JavaMail库的版本(mail.jar或javax.mail.jar)。也许您需要一个更新的。 从此处下载最新版本:https://javaee.github.io/javamail/

答案 15 :(得分:-1)

这是工作解决方案兄弟。这是有保障的

1)首先打开您要发送邮件的Gmail帐户,例如“xyz@gmail.com”

2)打开https://support.google.com/accounts/answer/6010255?hl=en

下面的链接

3)点击“转到我的帐户中的”安全性较低的应用“部分。”选项

4)然后打开它

5)就是这样(:

这是我的代码

1 apple A,C,D,F  
2 pear A,D  
3 plum B,F  
4 banana K,P,Z  
5 orange B,C,D,H