您的位置:首页 > 编程语言 > Java开发

javamail发邮件使用ssl

2018-03-30 16:58 387 查看
ssl 端口465

使用spring boot 架构

在yml文件中添加配置

javaMailProperties:

mail.smtp.ssl.enable: true

注意 :配置文件属性为javaMailProperties中添加mail.smtp.ssl.enable: true

StmpTransport 构造方法根据properties 变量中设置的mail.smtp.ssl.enable 来初始化isSSL属性为true

protected SMTPTransport(Session session, URLName urlname,
String name, boolean isSSL) {
super(session, urlname);
logger = new MailLogger(this.getClass(), "DEBUG SMTP", session);
traceLogger = logger.getSubLogger("protocol", null);
noauthdebug = !PropUtil.getBooleanSessionProperty(session,
"mail.debug.auth", false);
if (urlname != null)
name = urlname.getProtocol();
this.name = name;
**if (!isSSL)
isSSL = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".ssl.enable", false);
if (isSSL)
this.defaultPort = 465;
else
this.defaultPort = 25;**
this.isSSL = isSSL;

// setting mail.smtp.quitwait to false causes us to not wait for the
// response from the QUIT command
quitWait = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".quitwait", true);

// mail.smtp.reportsuccess causes us to throw an exception on success
reportSuccess = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".reportsuccess", false);

// mail.smtp.starttls.enable enables use of STARTTLS command
useStartTLS = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".starttls.enable", false);

// mail.smtp.starttls.required requires use of STARTTLS command
requireStartTLS = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".starttls.required", false);

// mail.smtp.userset causes us to use RSET instead of NOOP
// for isConnected
useRset = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".userset", false);

// mail.smtp.noop.strict requires 250 response to indicate success
noopStrict = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".noop.strict", true);

// check if SASL is enabled
enableSASL = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".sasl.enable", false);
if (enableSASL)
logger.config("enable SASL");
useCanonicalHostName = PropUtil.getBooleanSessionProperty(session,
"mail." + name + ".sasl.usecanonicalhostname", false);
if (useCanonicalHostName)
logger.config("use canonical host name");

// created here, because they're inner classes that reference "this"
Authenticator[] a = new Authenticator[] {
new LoginAuthenticator(),
new PlainAuthenticator(),
new DigestMD5Authenticator(),
new NtlmAuthenticator(),
new OAuth2Authenticator()
};
StringBuffer sb = new StringBuffer();
for (int i = 0; i < a.length; i++) {
authenticators.put(a[i].getMechanism(), a[i]);
sb.append(a[i].getMechanism()).append(' ');
}
defaultAuthenticationMechanisms = sb.toString();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java