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

你可能感兴趣的:(java)