【问题标题】:My Java program stopped sending emails using my gmail Account我的 Java 程序停止使用我的 gmail 帐户发送电子邮件
【发布时间】:2013-06-13 22:30:18
【问题描述】:

我在这里和其他地方搜索了几个相关的帖子,但没有一个能解决我的问题。我有一个使用“javamail API”向一组人发送电子邮件的程序。它工作正常一次。今天我又需要了,但我无法发送任何电子邮件...我的 sendEmail 方法如下:

public void sendEmail(String userName, String password, String toAddress, 
       String subject, String message, String[] attachFiles) 
       throws AddressException, MessagingException {

     // sets SMTP properties
    Properties properties = new Properties();
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.user", userName);
    properties.put("mail.password", password);

    // creates a new session with an authenticator
    Authenticator auth = new SMTPAuthenticator(userName, password);
    Session session = Session.getInstance(properties, auth);

    // creates a new e-mail message
    MimeMessage msg = new MimeMessage(session);

    try {
        msg.setFrom(new InternetAddress(userName, "My name"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    InternetAddress[] toAddresses = {new InternetAddress(toAddress)};        
    msg.setRecipients(Message.RecipientType.TO, toAddresses);
    msg.setSubject(subject);
    msg.setSentDate(new Date());

    // creates message part
    MimeBodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setContent(message, "text/html");

    // creates multi-part
    Multipart multipart = new MimeMultipart();
    multipart.addBodyPart(messageBodyPart);       

    // adds attachments
    if (attachFiles != null && attachFiles.length > 0) {
        for (String filePath : attachFiles) {
            addAttachment(multipart, filePath);
        }
    }

    // sets the multi-part as e-mail's content
    msg.setContent(multipart);

    // sends the e-mail
    Transport.send(msg);

}

所以,现在尝试调用此方法时出现以下错误 (我使用的是 jdk 1.7.0_21):

Sending email Failed...

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:367)
    at javax.mail.Service.connect(Service.java:226)
    at javax.mail.Service.connect(Service.java:175)
    at javax.mail.Transport.send0(Transport.java:253)
    at javax.mail.Transport.send(Transport.java:124)
    at EmailSender.sendEmail(EmailSender.java:86)
    at CFP_LaWasp_EmailSender.sendCFPLaWasp(CFP_LaWasp_EmailSender.java:178)
    at CFP_LaWasp_EmailSender.main(CFP_LaWasp_EmailSender.java:220)
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 sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
    at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
    at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
    at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
    at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
    at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
    at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:528)
    at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:465)
    at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
    ... 9 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:385)
    at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
    at sun.security.validator.Validator.validate(Validator.java:260)
    at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
    at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
    ... 19 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:196)
    at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
    at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
    ... 25 more

我不知道如何解决这个问题,因为它以前可以工作......另外,我不知道我的 Gmail 是否与此有关(如果它阻止了此访问......)

感谢您的帮助。

【问题讨论】:

    标签: gmail jakarta-mail


    【解决方案1】:

    关键错误是这样的:

    原因:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径

    this JavaMail FAQ entry

    由于您要连接到 Gmail,因此不应发生这种情况。最可能的原因是:

    • 防火墙或防病毒程序拦截了您的请求。
    • 您的 JDK 安装出现问题,无法找到受信任的证书颁发机构
    • 您正在运行的应用程序服务器已覆盖 JDK 的受信任证书颁发机构列表

    【讨论】:

    • 终于找到问题了!这是阻止应用程序的防病毒软件。非常感谢!
    • 你的建议很有帮助:-)
    • 这个帮助了...在我的情况下,avast 正在这样做。
    • 谢谢,我花了 2 个小时才找到解决“PKIX 路径构建失败”的解决方案,终于,这该死的 avast。
    • 我也是 Avast。以下是解决方法:warriorforum.com/main-internet-marketing-discussion-forum/…
    【解决方案2】:

    我已经为这个错误苦苦挣扎了几个小时。我尝试使用 Bil Shannon 提供的答案安装服务器证书。没有任何效果。

    我的问题是 AVAST Antivirus。禁用 Avast Mail Shield 后,我就可以从我的 Web 应用程序通过 gmail 帐户发送邮件了。

    【讨论】:

      【解决方案3】:

      我在使用 java 8 时遇到了这个问题。更新此属性后问题解决了

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

      如果在 application.property 中使用了 spring boot

      spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com

      【讨论】:

        【解决方案4】:

        我的问题已经解决了:

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

        当我使用弹簧靴时

        spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com
        

        【讨论】:

          猜你喜欢
          • 2019-04-05
          • 2012-03-30
          • 2017-08-05
          • 2012-05-25
          • 2019-02-04
          • 2012-03-16
          • 1970-01-01
          • 2017-12-21
          • 1970-01-01
          相关资源
          最近更新 更多