java 如何正确设置 javamail 以使用 STARTTLS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15619349/
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-10-31 20:17:06  来源:igfitidea点击:

How do I properly setup javamail to use STARTTLS?

javasmtpjavamailstarttls

提问by Knoxie

When setting up my session I am setting the starttls.enable and .required properties but when the connection happens it should fail according to the documentation:

在设置我的会话时,我正在设置 starttls.enable 和 .required 属性,但是当连接发生时,它应该会根据文档失败:

mail.smtp.starttls.enable boolean If true, enables the use of the STARTTLS command (if supported by the server) to switch the connection to a TLS-protected connection before issuing any login commands. Note that an appropriate trust store must configured so that the client will trust the server's certificate. Defaults to false." http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html

mail.smtp.starttls.enable boolean 如果为 true,则启用 STARTTLS 命令(如果服务器支持)在发出任何登录命令之前将连接切换到受 TLS 保护的连接。请注意,必须配置适当的信任库,以便客户端信任服务器的证书。默认为 false。” http://javamail.kenai.com/nonav/javadocs/com/sun/mail/smtp/package-summary.html

props = new Properties();
props.put("mail.smtps.host", MAILSERV);
props.put("mail.smtps.socketFactory.port", 465);
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.port", 465);
props.put("mail.smtps.socketFactory.fallback", "false");
props.put("mail.smtps.socketFactory.class","utils.DummySSLSocketFactory");
props.put("mail.smtps.quitwait", "false");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.starttls.required", "true");
Session session = Session.getInstance(props, authenticator);  


transport = session.getTransport("smtps");
transport.connect(mailServer, port, username, password);
transport.sendMessage(message, message.getAllRecipients());

So what am I doing wrong that is allowing me to use Starttls when its not supported by the server?

那么我做错了什么,允许我在服务器不支持的情况下使用 Starttls?

回答by Knoxie

The issue I was having with STARTTLS was caused by me getting a secure transport:

我在使用 STARTTLS 时遇到的问题是由于我获得了安全传输:

transport = session.getTransport("smtps");

After changing it to "smtp" i was able to use STARTTLS.

将其更改为“smtp”后,我可以使用 STARTTLS。