使用 TSL 通过 SMTP 发送电子邮件

发布于 2024-11-29 17:48:18 字数 1906 浏览 0 评论 0原文

下面发布的代码可以很好地帮助我通过带有 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);
    }
}
}

The code posted below works fine for me to send an email over an STMP with SSL.
Now the SMTP changed to TSL and i dont manage to send email with it.
I tried several things like adding

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

but it was no use.

The error code says:

"javax.mail.MessagingException: Could not connect to SMTP host: exch.studi.fhws.de, port: 587;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?"

Any ideas?

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);
    }
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小糖芽 2024-12-06 17:48:18

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

The error message says you're connecting to port 587 - this is an SSL-enabled email port, which means the connection has to be SSL from the get-go. Your code is connecting via plaintext (e.g. plain port 25) and THEN attempting to start TLS. This won't work, as port 587 expects an SSL exchange immediately, not a "EHLO ... / STARTTLS" plaintext command set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文