【问题标题】:Sending emails over SMTP with TSL使用 TSL 通过 SMTP 发送电子邮件
【发布时间】:2011-10-28 05:03:32
【问题描述】:

下面发布的代码适用于我通过带有 SSL 的 STMP 发送电子邮件。 现在 SMTP 更改为 TSL,我无法使用它发送电子邮件。 我尝试了几件事,比如添加

props.put("mail.smtp.starttls.enable","true");

但是没有用。

错误代码说:

“javax.mail.MessagingException:无法连接到 SMTP 主机:exch.studi.fhws.de,端口:587; 嵌套异常是: javax.net.ssl.SSLException:无法识别的 SSL 消息,明文连接?”

有什么想法吗?

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class SendMail
{
String d_email = "...",
password = "...",
host = "...",
port = "25",
mailTo = "...",
mailSubject = "test",
mailText = "test";

public SendMail()
{
    Properties props = new Properties();
    props.put("mail.smtp.user", d_email);
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.port", port);
    props.put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    SecurityManager security = System.getSecurityManager();

    try
    {
        Authenticator auth = new SMTPAuthenticator();
        Session session = Session.getInstance(props, auth);

        MimeMessage msg = new MimeMessage(session);
        msg.setText(mailText);
        msg.setSubject(mailSubject);
        msg.setFrom(new InternetAddress(d_email));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(mailTo));
        Transport.send(msg);
    }
    catch (Exception mex)
    {
        mex.printStackTrace();
    }
}

public static void main(String[] args)
{
    TextClass tc = new TextClass();
}

private class SMTPAuthenticator extends javax.mail.Authenticator
{
    public PasswordAuthentication getPasswordAuthentication()
    {
        return new PasswordAuthentication(d_email, password);
    }
}
}

【问题讨论】:

标签: java email smtp jakarta-mail ssl


【解决方案1】:

错误消息显示您正在连接到端口 587 - 这是一个启用 SSL 的电子邮件端口,这意味着连接必须从一开始就使用 SSL。您的代码通过纯文本(例如纯端口 25)连接,然后尝试启动 TLS。这不起作用,因为端口 587 需要立即进行 SSL 交换,而不是“EHLO ... / STARTTLS”明文命令集。

【讨论】:

  • 谢谢,错误消息中列出的端口 587 来自我编辑端口的另一次尝试。那么你认为端口本身是问题,还是有其他问题?我尝试了很多,比如 25,265 和 587。
【解决方案2】:

也许这个关于禁用 SMTP 端口的 Exchange 服务器的答案很有用:https://stackoverflow.com/a/12631772/187148

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-06
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 2016-01-16
    • 2016-04-16
    相关资源
    最近更新 更多