JavaMail mail.smtp.ssl.enable 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2043018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:55:38  来源:igfitidea点击:

JavaMail mail.smtp.ssl.enable is not working

javassljavamail

提问by

I read on several sites that, when using the JavaMail API, to set the property mail.smtp.ssl.enableto true. I have some code as follows:

我在几个站点上读到,在使用 JavaMail API 时,将属性设置mail.smtp.ssl.enable为 true。我有一些代码如下:

props.put("mail.smtp.host", "exchangemail1.example.com");
props.put("mail.from", "[email protected]");
props.put("mail.smtp.starttls.enable", "true");
// I tried this by itself and also together with ssl.enable)
props.put("mail.smtp.ssl.enable", "true");

Session session = Session.getInstance(props, null);
MimeMessage msg = new MimeMessage(session);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, "[email protected]");  
    // also tried @gmail.com
msg.setSubject("JavaMail ssl test");
msg.setSentDate(new Date());
msg.setText("Hello, world!\n");
props.put("mail.smtp.auth", "false");

Transport trnsport;
trnsport = session.getTransport("smtp");
trnsport.connect();
msg.saveChanges(); 
trnsport.sendMessage(msg, msg.getAllRecipients());
trnsport.close();

This sends email, but:

这会发送电子邮件,但是:

  1. when I do traffic capture, I see it is not encrypted
  2. When using debug (props.put("mail.debug", "true")), I see that "isSSL false"
  1. 当我进行流量捕获时,我看到它没有加密
  2. 使用 debug ( props.put("mail.debug", "true")) 时,我看到“isSSL false”

(I also tried above adding in props.put("mail.smtp.auth","true")+ user/password....)

(我也尝试在上面添加props.put("mail.smtp.auth","true")+ 用户/密码....)

Any ideas what I am doing wrong?

任何想法我做错了什么?

回答by Bozho

I'd suggest using Apache commons-email. It has setters for the most used properties (including SSL / TLS) and is more friendlier to use and sits ontop of the JavaMail API.

我建议使用 Apache commons-email。它具有最常用属性(包括 SSL / TLS)的设置器,使用起来更友好,并且位于 JavaMail API 之上。

Update: I was looking at commons-email code, and saw these lines:

更新:我正在查看 commons-email 代码,并看到以下几行:

properties.setProperty("mail.smtp.starttls.enable", this.tls);
properties.setProperty("mail.smtp.auth", "true");

so, give these properties a try as well.

所以,也试试这些属性吧。

回答by xplanner expert

To use SSL you should change your protocol from SMTPto SMTPSby changing

要使用 SSL,您应该通过更改将您的协议从SMTP更改为SMTPS

trnsport = session.getTransport("smtp");

to

trnsport = session.getTransport("smtps");

回答by Ralph

Form the Java Doc:

形成Java 文档

Note that if you're using the "smtps" protocol to access SMTP over SSL, all the properties would be named "mail.smtps.*".

请注意,如果您使用“smtps”协议通过 SSL 访问 SMTP,则所有属性都将命名为“mail.smtps.*”。

回答by Visruth

Try

尝试

props.put("mail.smtp.auth", "true");
props.setProperty("mail.smtp.**ssl.enable", "true");
props.setProperty("mail.smtp.**ssl.required", "true");

回答by lanoxx

This is from the SSLNOTESdocument from JavaMail (emphasis added):

这是来自JavaMail的SSLNOTES文档(强调已添加):

First, and perhaps the simplest, is to set a property to enable use of SSL. For example, to enable use of SSL for SMTP connections, set the property "mail.smtp.ssl.enable"to "true".

Alternatively, you can configure JavaMail to use one of the SSL-enabled protocol names. In addition to the non-SSL JavaMail protocols "imap", "pop3", and "smtp", the protocols "imaps", "pop3s", and "smtps"can be used to connect to the corresponding services using an SSL connection.

-- STARTTLS support

The STARTTLS support is available in the standard "imap" and "smtp" protocols, but must be enabled by setting the appropriate property, mail.imap.starttls.enable or mail.smtp.starttls.enable, to "true". When set, if the server supports the STARTTLS command, it will be used after making the connection and before sending any login information.

首先,也许是最简单的方法是设置一个属性以启用 SSL。例如,要为 SMTP 连接启用 SSL,请将属性“mail.smtp.ssl.enable”设置为“true”。

或者,您可以将 JavaMail 配置为使用启用 SSL 的协议名称之一。除了非 SSL JavaMail 协议“imap”、“pop3”和“smtp”之外,还可以使用协议“imaps”、“pop3s”和“smtps”通过 SSL 连接来连接到相应的服务。

-- STARTTLS 支持

STARTTLS 支持在标准的“imap”和“smtp”协议中可用,但必须通过将适当的属性 mail.imap.starttls.enable 或mail.smtp.starttls.enable设置为“true”来启用。设置后,如果服务器支持 STARTTLS 命令,它将在建立连接后和发送任何登录信息之前使用。

So when using the STARTTLS support it seems that it is not necessary to set the protocol to smtps.

因此,当使用 STARTTLS 支持时,似乎没有必要将协议设置为smtps.