javamail发邮件使用ssl

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();
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值