1

我目前正在调查 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

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

编辑:

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

4

1 回答 1

2

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

此外,您使用的是相当旧的 JavaMail 版本。您可能想要升级到最新版本

于 2014-09-29T22:47:52.560 回答