69

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

返回此错误

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
4

19 回答 19

149
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
于 2013-07-23T00:34:05.287 回答
35

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

于 2015-06-24T05:03:00.007 回答
33

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

于 2013-12-25T13:02:25.607 回答
23

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

mail.smtp.ssl.protocols=TLSv1.2

于 2021-01-07T15:46:08.470 回答
11

@carlos的第一个答案对我有用:

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

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

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

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

于 2018-01-04T13:43:49.113 回答
8

在我的情况下,问题是通过删除该行来解决的

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

这可能是因为电子邮件服务器上的一些 ssl 配置错误,我不确定。电子邮件服务器管理员从不承认这一点,总是责怪托管服务提供商 :)

于 2019-08-14T11:41:08.103 回答
8

经过一整天的搜索,我禁用了 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. 单击“疑难解答”,然后单击“打开旧设置”

在此处输入图像描述

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

在此处输入图像描述

就我而言,我刚刚从 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
于 2020-01-22T15:39:50.270 回答
5

上面的 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
于 2020-07-17T20:16:16.330 回答
2

如果应用程序不支持SMTP 主机正在使用的TLS 版本,也可能出现这种情况。

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

于 2019-08-22T14:04:00.057 回答
2

尝试将端口更改为 465

mail.smtp.socketFactory.port=465
mail.smtp.port=465
于 2019-07-11T18:26:01.693 回答
1

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

于 2015-08-12T04:28:53.657 回答
1

是的,它适用于本地主机:

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

该错误仅在本地主机上引发,因此您也应该在远程服务器上尝试。通常,上面提到的代码片段在没有ssl.trust属性的情况下运行良好

于 2021-01-06T04:44:20.970 回答
0

下面的一组属性application.properties对我有用:

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<gmail_id>
spring.mail.password=<gmail_password>
spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

请注意,如果您为您的 Gmail 帐户启用了两步验证,那么gmail_password上述配置中的密码不应是普通密码,而是为您的 Google 帐户生成的 16 个字符的应用程序密码。按照此链接查看详细信息并生成您的 Google 应用密码。

于 2021-08-17T11:10:32.407 回答
0

就我而言,我使用的是OpenJDK,当我切换OracleJDK时,问题得到了解决。

于 2021-10-12T07:01:41.570 回答
0

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

于 2020-12-07T13:59:10.820 回答
0

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

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

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

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

4)然后打开它

5)就是这样(:

这是我的代码

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class SendEmail {

   final String senderEmailID = "Sender Email id";
final String senderPassword = "Sender Pass word";
final String emailSMTPserver = "smtp.gmail.com";
final String emailServerPort = "465";
String receiverEmailID = null;
static String emailSubject = "Test Mail";
static String emailBody = ":)";
public SendEmail(String receiverEmailID, String emailSubject, String emailBody)
{
this.receiverEmailID=receiverEmailID;
this.emailSubject=emailSubject;
this.emailBody=emailBody;
Properties props = new Properties();
props.put("mail.smtp.user",senderEmailID);
props.put("mail.smtp.host", emailSMTPserver);
props.put("mail.smtp.port", emailServerPort);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", emailServerPort);
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
MimeMessage msg = new MimeMessage(session);
msg.setText(emailBody);
msg.setSubject(emailSubject);
msg.setFrom(new InternetAddress(senderEmailID));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(receiverEmailID));
Transport.send(msg);
System.out.println("Message send Successfully:)");
}
catch (Exception mex)
{
mex.printStackTrace();
}
}
public class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(senderEmailID, senderPassword);
}
}
    public static void main(String[] args) {
       SendEmail mailSender;
        mailSender = new SendEmail("Receiver Email id","Testing Code 2 example","Testing Code Body yess");
    }

}
于 2015-11-17T14:40:10.850 回答
0

这取决于“嵌套异常”。如果嵌套异常是“没有适当的协议”,则在系统属性中指定协议:

-Dmail.smtp.ssl.protocols=TLSv1.2

我看到有些人建议在创建 STMP 会话的代码中设置它,但是如果您想在将来更新该值,则需要进行修补和部署。使用系统属性允许您在不更改代码的情况下对其进行更新。

于 2021-12-13T01:58:34.030 回答
-1

尝试使用 JavaMail 附带的 smtpsend 程序,如此处所述。如果以同样的方式失败,则说明您的 JDK 配置或网络配置有问题。

于 2013-04-20T23:16:22.853 回答
-1

是什么帮助我解决了这个问题,并且在此之前我已经尝试了所有方法,就是将我安装的 jre 配置为 JRE 1.8。

Eclipse 中的步骤:Windows>preferences>java>installed JRE>jre1.8.0

如果设置为 jdk,则切换到 jre(这是最新 Java 版本默认设置的)。

于 2017-06-11T19:07:09.373 回答