使用TSL通过SMTP发送电子邮件 [英] Sending emails over SMTP with TSL

查看:392
本文介绍了使用TSL通过SMTP发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下发布的代码对我来说可以通过带有SSL的STMP发送电子邮件。
现在SMTP更改为TSL,我不会设法发送电子邮件。
我尝试过几个东西,比如添加

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

但没有用。

错误代码说:

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

"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?"

任何想法?

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


推荐答案

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

Maybe this answer, talking about Exchange servers with SMTP ports disabled, is useful: https://stackoverflow.com/a/12631772/187148

这篇关于使用TSL通过SMTP发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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