Java Mail:在没有 SSL 的端口 25 上发送电子邮件时出现 SSLHandshakeException [英] Java Mail: SSLHandshakeException when sending email on port 25 without SSL

查看:33
本文介绍了Java Mail:在没有 SSL 的端口 25 上发送电子邮件时出现 SSLHandshakeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我试图在没有 SSL 的情况下在端口 25 上发送电子邮件,但我收到了 SSLHandshakeException.您可以在下面看到 javax.mail.Session 和 SSLHandshakeException 打印的调试消息.

你能帮我理解发生了什么吗?

<上一页>2009 年 12 月 22 日 15:13:21,054 信息根目录:197 - 调试:setDebug:JavaMail 版本 1.4ea2009 年 12 月 22 日 15:13:21,059 INFO root:197 - 调试:getProvider() 返回 javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]2009 年 12 月 22 日 15:13:21,103 信息根:197 - 调试 SMTP:useEhlo true,useAuth false2009 年 12 月 22 日 15:13:21,103 信息根目录:197 - 调试 SMTP:尝试连接到主机mail.abcdef.com",端口 25,isSSL 错误2009 年 12 月 22 日 15:13:21,338 信息根目录:197 - 220 mail.uvwxyz.com ESMTP abcdef 邮件服务器 v8.0;2009 年 12 月 22 日星期二 15:12:10 -07002009 年 12 月 22 日 15:13:21,338 信息根目录:197 - 调试 SMTP:连接到主机mail.abcdef.com",端口:252009 年 12 月 22 日 15:13:21,342 信息根:197 - EHLO fsb1602009 年 12 月 22 日 15:13:21,383 INFO root:197 - 250-mail.uvwxyz.com 你好 fsb160 [65.124.157.54],很高兴见到你.250 增强状态代码250 尺寸250-EXPN250-ETRN250-ATRN250-DSN250 个检查点250-8BITMIME250-AUTH CRAM-MD5 DIGEST-MD5 登录明文250-授权=登录250-STARTTLS250 帮助2009 年 12 月 22 日 15:13:21,383 信息根目录:197 - 调试 SMTP:找到扩展名ENHANCEDSTATUSCODES",arg"2009 年 12 月 22 日 15:13:21,383 信息根目录:197 - 调试 SMTP:找到扩展名SIZE",arg"2009 年 12 月 22 日 15:13:21,384 信息根目录:197 - 调试 SMTP:找到扩展名EXPN",arg"2009 年 12 月 22 日 15:13:21,384 信息根目录:197 - 调试 SMTP:找到扩展名ETRN",arg"2009 年 12 月 22 日 15:13:21,384 信息根目录:197 - 调试 SMTP:找到扩展名ATRN",arg"2009 年 12 月 22 日 15:13:21,384 信息根目录:197 - 调试 SMTP:找到扩展名DSN",arg"2009 年 12 月 22 日 15:13:21,385 信息根目录:197 - 调试 SMTP:找到扩展名CHECKPOINT",arg"2009 年 12 月 22 日 15:13:21,385 信息根目录:197 - 调试 SMTP:找到扩展名8BITMIME",arg"2009 年 12 月 22 日 15:13:21,385 信息根目录:197 - 调试 SMTP:找到扩展名AUTH",argCRAM-MD5 DIGEST-MD5 LOGIN PLAIN"2009 年 12 月 22 日 15:13:21,385 信息根目录:197 - 调试 SMTP:找到扩展名AUTH=LOGIN",arg"2009 年 12 月 22 日 15:13:21,386 信息根目录:197 - 调试 SMTP:找到扩展名STARTTLS",arg"2009 年 12 月 22 日 15:13:21,386 信息根目录:197 - 调试 SMTP:找到扩展帮助",arg"2009 年 12 月 22 日 15:13:21,389 信息根:197 - STARTTLS2009 年 12 月 22 日 15:13:21,430 INFO root:197 - 220 2.0.0 准备启动 TLS2009 年 12 月 22 日 15:13:21,809 信息根:197 - EHLO fsb1602009 年 12 月 22 日 15:13:21,886 WARN E:314 - 无法发送电子邮件javax.mail.MessagingException:无法向 SMTP 主机发送命令;嵌套异常是:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径在 com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1420)在 com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408)在 com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847)在 com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384)在 javax.mail.Service.connect(Service.java:275)在 javax.mail.Service.connect(Service.java:156)在 java.lang.Thread.run(未知来源)原因:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径在 com.sun.net.ssl.internal.ssl.Alerts.getSSLException(未知来源)在 com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(未知来源)在 com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(未知来源)在 com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(未知来源)在 com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(未知来源)在 com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(未知来源)在 com.sun.net.ssl.internal.ssl.Handshaker.processLoop(未知来源)在 com.sun.net.ssl.internal.ssl.Handshaker.process_record(未知来源)在 com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(未知来源)在 com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(未知来源)在 com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(未知来源)在 com.sun.net.ssl.internal.ssl.AppOutputStream.write(未知来源)在 com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101)在 java.io.BufferedOutputStream.flushBuffer(未知来源)在 java.io.BufferedOutputStream.flush(未知来源)在 com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1418)... 7 更多原因:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径在 sun.security.validator.PKIXValidator.doBuild(未知来源)在 sun.security.validator.PKIXValidator.engineValidate(未知来源)在 sun.security.validator.Validator.validate(未知来源)在 com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(未知来源)在 com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(未知来源)在 com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(未知来源)... 19 更多原因:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径在 sun.security.provider.certpath.SunCertPathBuilder.engineBuild(未知来源)在 java.security.cert.CertPathBuilder.build(未知来源)... 25 更多

用于创建会话和连接服务器的代码:

<上一页>会话会话 = EmailUtilities.createSession(smtpServer, smtpPort, smtpSSL, smtpAuthentication);传输传输 = session.getTransport();transport.connect(smtpServer, smtpUser, smtpPassword);尝试 {MimeMessage message = EmailUtilities.createMessage(Main.APPLICATION_NAME + " " + Main.APPLICATION_VERSION, session, to, from, subject, body, attachmentName, attachment, "application/pdf");transport.sendMessage(消息,message.getAllRecipients());} 捕捉(异常 e){抛出新异常(e);} 最后 {运输.close();}公共静态会话 createSession(字符串 smtpServer,整数 smtpPort,布尔 ssl,布尔验证){字符串协议 = "smtp";//$非NLS-1$如果(SSL){协议 = "smtps";//$非NLS-1$}//-- 附加到默认会话,或者我们可以启动一个新会话 --属性 props = new Properties();props.put("mail.transport.protocol", 协议);props.put("mail." + 协议 + ".host", smtpServer);props.put("mail." + 协议 + ".port", smtpPort.toString());props.put("mail." + 协议 + ".starttls.enable", Boolean.TRUE.toString());如果(认证){props.put("mail." + 协议 + ".auth", Boolean.TRUE.toString());}会话会话 = Session.getDefaultInstance(props, null);session.setDebug(true);返回会话;}

解决方案

确保您没有在属性中启用 starttls (mail.smtp.starttls.enable=true)

Although I'm trying to send an email on port 25 without SSL, I'm getting an SSLHandshakeException. Below you can see debug messages printed by javax.mail.Session and the SSLHandshakeException.

Can you help me understand what's going on?

22 Dec 2009 15:13:21,054  INFO root:197 - DEBUG: setDebug: JavaMail version 1.4ea
22 Dec 2009 15:13:21,059  INFO root:197 - DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
22 Dec 2009 15:13:21,103  INFO root:197 - DEBUG SMTP: useEhlo true, useAuth false
22 Dec 2009 15:13:21,103  INFO root:197 - DEBUG SMTP: trying to connect to host "mail.abcdef.com", port 25, isSSL false
22 Dec 2009 15:13:21,338  INFO root:197 - 220 mail.uvwxyz.com ESMTP abcdef Mail Server v8.0; Tue, 22 Dec 2009 15:12:10 -0700
22 Dec 2009 15:13:21,338  INFO root:197 - DEBUG SMTP: connected to host "mail.abcdef.com", port: 25
22 Dec 2009 15:13:21,342  INFO root:197 - EHLO fsb160
22 Dec 2009 15:13:21,383  INFO root:197 - 250-mail.uvwxyz.com Hello fsb160 [65.124.157.54], pleased to meet you.
250-ENHANCEDSTATUSCODES
250-SIZE
250-EXPN
250-ETRN
250-ATRN
250-DSN
250-CHECKPOINT
250-8BITMIME
250-AUTH CRAM-MD5 DIGEST-MD5 LOGIN PLAIN
250-AUTH=LOGIN
250-STARTTLS
250 HELP
22 Dec 2009 15:13:21,383  INFO root:197 - DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
22 Dec 2009 15:13:21,383  INFO root:197 - DEBUG SMTP: Found extension "SIZE", arg ""
22 Dec 2009 15:13:21,384  INFO root:197 - DEBUG SMTP: Found extension "EXPN", arg ""
22 Dec 2009 15:13:21,384  INFO root:197 - DEBUG SMTP: Found extension "ETRN", arg ""
22 Dec 2009 15:13:21,384  INFO root:197 - DEBUG SMTP: Found extension "ATRN", arg ""
22 Dec 2009 15:13:21,384  INFO root:197 - DEBUG SMTP: Found extension "DSN", arg ""
22 Dec 2009 15:13:21,385  INFO root:197 - DEBUG SMTP: Found extension "CHECKPOINT", arg ""
22 Dec 2009 15:13:21,385  INFO root:197 - DEBUG SMTP: Found extension "8BITMIME", arg ""
22 Dec 2009 15:13:21,385  INFO root:197 - DEBUG SMTP: Found extension "AUTH", arg "CRAM-MD5 DIGEST-MD5 LOGIN PLAIN"
22 Dec 2009 15:13:21,385  INFO root:197 - DEBUG SMTP: Found extension "AUTH=LOGIN", arg ""
22 Dec 2009 15:13:21,386  INFO root:197 - DEBUG SMTP: Found extension "STARTTLS", arg ""
22 Dec 2009 15:13:21,386  INFO root:197 - DEBUG SMTP: Found extension "HELP", arg ""
22 Dec 2009 15:13:21,389  INFO root:197 - STARTTLS
22 Dec 2009 15:13:21,430  INFO root:197 - 220 2.0.0 Ready to start TLS
22 Dec 2009 15:13:21,809  INFO root:197 - EHLO fsb160
22 Dec 2009 15:13:21,886  WARN E:314 - Failed to send email
javax.mail.MessagingException: Can't send command to SMTP host;
  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.sendCommand(SMTPTransport.java:1420)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1408)
    at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:847)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:384)
    at javax.mail.Service.connect(Service.java:275)
    at javax.mail.Service.connect(Service.java:156)
    at java.lang.Thread.run(Unknown Source)
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(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source)
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
    at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source)
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
    at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
    at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source)
    at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:101)
    at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
    at java.io.BufferedOutputStream.flush(Unknown Source)
    at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1418)
    ... 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(Unknown Source)
    at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
    at sun.security.validator.Validator.validate(Unknown Source)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
    ... 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(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
    ... 25 more

The code used to create the session and to connect to the server:

Session session = EmailUtilities.createSession(smtpServer, smtpPort, smtpSSL, smtpAuthentication);

Transport transport = session.getTransport();
transport.connect(smtpServer, smtpUser, smtpPassword);

try {
    MimeMessage message = EmailUtilities.createMessage(Main.APPLICATION_NAME + " " + Main.APPLICATION_VERSION, session, to, from, subject, body, attachmentName, attachment, "application/pdf");
    transport.sendMessage(message, message.getAllRecipients());
} catch (Exception e) {
    throw new Exception(e);
} finally {
    transport.close();
}


public static Session createSession(String smtpServer, Integer smtpPort, boolean ssl, boolean authenticate) {
    String protocol = "smtp"; //$NON-NLS-1$
    if (ssl) {
    protocol = "smtps"; //$NON-NLS-1$
    }

    // -- Attaching to default Session, or we could start a new one --
    Properties props = new Properties();
    props.put("mail.transport.protocol", protocol);
    props.put("mail." + protocol + ".host", smtpServer);
    props.put("mail." + protocol + ".port", smtpPort.toString());
    props.put("mail." + protocol + ".starttls.enable", Boolean.TRUE.toString());

    if (authenticate) {
        props.put("mail." + protocol + ".auth", Boolean.TRUE.toString());
    }

    Session session = Session.getDefaultInstance(props, null);
    session.setDebug(true);

    return session;
}

解决方案

Make sure you haven't enabled starttls in your properties (mail.smtp.starttls.enable=true)

这篇关于Java Mail:在没有 SSL 的端口 25 上发送电子邮件时出现 SSLHandshakeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆