【问题标题】:Could not connect to SMTP host无法连接到 SMTP 主机
【发布时间】:2014-05-01 22:07:03
【问题描述】:

我正在尝试通过我们的私人 smtp 服务器发送电子邮件,这是我的代码

  public static void main(String args[]) throws MessagingException {
  Properties props = System.getProperties();
        props.put("mail.smtps.host", "gi-systems.net");
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.port", "25");
        Session session = Session.getInstance(props, null);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("xxxx@gi-systems.net"));
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("yyyy@hotmail.com, false));
        msg.setSubject("Password Recovery");
        msg.setText(pwd);
        msg.setSentDate(new Date());
        SMTPTransport t = (SMTPTransport) session.getTransport("smtps");
        t.connect("smtp.gi-systems.net", "xxxx@gi-systems.net", "mypassword");
        t.sendMessage(msg, msg.getAllRecipients());
        System.out.println("Response: " + t.getLastServerResponse());
        t.close();
}

但是当我运行这段代码时,我得到了这个异常

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gi-systems.net, port: 465;
  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.openServer(SMTPTransport.java:1706)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at com.mycompany.sendmail.App.SendEmail(App.java:45)
    at com.mycompany.sendmail.App.main(App.java:64)
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)
......

Ps:当我使用 gmail smtp 和电子邮件地址时,它工作正常

我该如何解决这个异常???

【问题讨论】:

    标签: java email ssl


    【解决方案1】:

    检查以下事项:

    例如 Gmail。

    1.

    t.connect("smtp.gmail.com", "mailID", "Password");
    

    2.属性:

    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");  //For Gmail 'smtp.gmail.com' check carefully for Yours               
    props.put("mail.smtp.port","587");//or port 465
    

    请检查您的情况。

    设置属性或身份验证似乎有问题。

    【讨论】:

    • 它适用于 gmailID 但不适用于 xxxxxx@gi-systems.com
    【解决方案2】:

    无法找到请求目标的有效认证路径

    这意味着您使用的证书机构是未知的。尝试使用 openssl 给出:

    $ openssl s_client -connect smtp.gi-systems.net:465
    ...
    Certificate chain
     0 s:/C=--/ST=France/L=Roubaix/O=OVH/OU=--/CN=ns317329.ip-37-187-133.eu/emailAddress=root@ns317329.ip-37-187-133.eu
       i:/C=--/ST=France/L=Roubaix/O=OVH/OU=--/CN=ns317329.ip-37-187-133.eu/emailAddress=root@ns317329.ip-37-187-133.eu
    ...
        Verify return code: 18 (self signed certificate)
    

    所以你使用的是自签名证书,当然它不能被 java 验证。 我不确定您是否可以通过指纹验证自签名证书,否则您可能需要创建自己的小型 CA(或使用 cacert.org 或类似名称)并将 CA 导入为受信任的,如 http://www.seamframework.org/Documentation/MakeSeammailWorkingWithYourSelfsignedCertificates 中所述

    【讨论】:

    • 这帮助我确定了配置(野生)证书的实际主机名。所以我改用那个名字而不是 www.xxx.tld 域名
    【解决方案3】:

    您确定端口地址和主机是否准确。我认为对于 SMTPS,一般使用 587 或 465 端口

    谢谢

    【讨论】:

    【解决方案4】:

    添加协议。

    props.put("mail.transport.protocol", "smtps");
    

    问候。

    【讨论】:

    • SMTPTransport t = (SMTPTransport) session.getTransport("smtps"); 没有按照你的建议做?
    • 运输!= 会话。这些道具用于会话,而不是仅用于传输。
    猜你喜欢
    • 1970-01-01
    • 2016-05-02
    • 2012-05-24
    • 2012-06-17
    • 2018-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多