【问题标题】:TLS SMTP Mail Java throws SSLHandShakeException [duplicate]TLS SMTP 邮件 Java 抛出 SSLHandShakeException [重复]
【发布时间】:2017-12-24 19:21:57
【问题描述】:

我正在尝试使用 SMTP 发送邮件,它设置为具有 25,587 个端口和 TLS 的默认 smtp 服务器。 MS Exchange 2010。所有身份验证数据都是正确的,因为我可以通过邮件的 Web 界面登录。下面的代码确实适用于 gmail smtp 服务器,但是当我使用自己的 smtp 服务器时,它会抛出证书路径错误:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:PKIX 路径构建失败: sun.security.provider.certpath.SunCertPathBuilderException:无法 找到请求目标的有效认证路径

public static void SendMailTLS(String to, String subject, String body) throws Exception{

    try {
                java.util.Properties props =  new java.util.Properties();
                props.put("mail.smtp.host", "sample");
                props.put("mail.smtp.port", "587");
                props.put("mail.smtp.starttls.enable", "true");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.connectiontimeout", "10000");    
                final String EmailUser = "sample";
                final String EmailPassword = "sample";    
                Session session = Session.getInstance(props, new javax.mail.Authenticator() {
                          protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(
                                EmailUser,EmailPassword);
                             }
                       });    session.setDebug(true);    
                InternetAddress fromAddress = new InternetAddress("sample",
                                        "sample");
                InternetAddress toAddress = new InternetAddress("sample",
                                        "sample");
                Message msg = new MimeMessage(session);
                msg.setFrom(fromAddress);
                msg.addRecipient(Message.RecipientType.TO,toAddress);
                msg.setSubject(subject);
                msg.setText(body);    
                Transport transport = session.getTransport("smtp");
                transport.connect();
                transport.sendMessage(msg, msg.getAllRecipients());
                } catch (MessagingException e) {e.printStackTrace();}
    }

我做错了吗?

【问题讨论】:

    标签: java ssl


    【解决方案1】:

    此错误消息意味着您的名为“sample”的服务器(MS Exchange 2010 服务器)正在使用您的 java 运行时不信任的证书。您可能正在使用自签名证书。因此,您需要使用 keytool 将此证书导入 Java 运行时证书存储。

    【讨论】:

      猜你喜欢
      • 2020-07-12
      • 2020-09-25
      • 2020-06-24
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 2021-07-17
      • 2011-11-29
      • 1970-01-01
      相关资源
      最近更新 更多