【问题标题】:Unrecognized SSL message while sending mail from localhost using STARTTLS使用 STARTTLS 从本地主机发送邮件时出现无法识别的 SSL 消息
【发布时间】:2014-09-25 12:14:58
【问题描述】:

我目前正在调查 James Mail Server,并在我的本地计算机上运行了一个实例。我正在尝试使用以下代码发送电子邮件,但遇到了下面列出的异常:

public static void send() {
    try {
        String host = "127.0.0.1";

        Properties props = System.getProperties();
        props.put("mail.host", host);
        props.put("mail.transport.protocol", "smtps");
        props.put("mail.smtps.auth", "true");
        props.put("mail.smtps.port", "465");
        props.put("mail.smtps.ssl.trust", host);

        Session mailSession = Session.getInstance(props);
        mailSession.setDebug(true);

        Message msg = new MimeMessage(mailSession);
        msg.setFrom(new InternetAddress(frAddress));
        InternetAddress[] address = { new InternetAddress(toAddress) };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject(subject);
        msg.setSentDate(new Date());
        msg.setText(message);

        Transport transport = mailSession.getTransport("smtps");
        transport.connect(host, userName, password);
        transport.sendMessage(msg, msg.getAllRecipients());
        transport.close();
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
    }
}

transport.connect(host, userName, password)遇到以下异常

DEBUG: setDebug: JavaMail version 1.4.2
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "127.0.0.1", port 465, isSSL true
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
javax.mail.MessagingException: Exception reading response;
  nested exception is:
    javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1764)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1523)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
    at javax.mail.Service.connect(Service.java:291)
    at javax.mail.Service.connect(Service.java:172)
    at com.spectramd.securemail.bouncycastle.JavaMailTest.send(JavaMailTest.java:50)
    at com.spectramd.securemail.bouncycastle.JavaMailTest.main(JavaMailTest.java:24)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.InputRecord.read(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.readDataRecord(Unknown Source)
    at com.sun.net.ssl.internal.ssl.AppInputStream.read(Unknown Source)
    at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:106)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:84)
    at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1742)
    ... 6 more

我已按照here 列出的步骤将 James 服务器配置为在 SSL 上启动,并按照步骤添加了一个虚拟 JKS 证书。我一直在网上寻找,但到目前为止,我无法找到解决方案。请帮忙。

编辑:

忘了提一下,我可以使用 James 附带的默认配置使用普通 SMTP 成功发送邮件。

【问题讨论】:

    标签: localhost jakarta-mail james starttls


    【解决方案1】:

    嗯,看来 James 的配置不正确。如果您使用 telnet 在端口 465 上连接它,您会收到纯文本响应吗?

    另外,您使用的是相当旧的 JavaMail 版本。您可能想升级到latest version

    【讨论】:

    • 嗯,这很奇怪!我正在使用 Java 1.6.0_27。不知道为什么 JavaMail 是 v1.4.2 !!!这已在我的 Eclipse 中已安装的 JRE 部分中得到验证,并在 cmd 处触发了 java -version 命令。同样telnet 127.0.0.1 465在詹姆斯输出屏幕上给我一个空白屏幕INFO 11:42:21,501 | james.smtpserver | Id='909048548' User='' Connection established from 127.0.0.1
    • 最新版本为1.5.2。为什么我给你指最新版本时你选择 1.4.7?
    • 哎呀没看到你的链接!!我刚刚用谷歌搜索了“下载 mail.jar”,它把我带到了那个版本的 oracle 网站!!!将使用 1.5.2。再次感谢!
    猜你喜欢
    • 2016-08-04
    • 1970-01-01
    • 2021-02-12
    • 2012-05-25
    • 1970-01-01
    • 2020-02-07
    • 2013-08-19
    • 2013-01-04
    • 1970-01-01
    相关资源
    最近更新 更多