“mail.smtp.auth”参数在 JavaMailSender 中的作用是什么?

What is the role of the "mail.smtp.auth" parameter in the JavaMailSender?

提问人:pizssn peng 提问时间:6/4/2023 更新时间:6/5/2023 访问量:324

问:

我使用 JavaMailSender 发送电子邮件。我在配置文件中配置了相应的用户名和密码,但发送电子邮件时出现错误。错误消息为 。org.apache.geronimo.javamail.transport.smtp.SMTPSendFailedException: authentication is required

这是我的配置文件

# Mail Configuration
management.health.mail.enabled=false
# Configure POP3 server
spring.mail.host=smtp.163.com
#User name and authorization code for sending emails
spring.mail.username=yy1*****[email protected]
spring.mail.password=YJ*****BSKF

# SSL Config
spring.mail.port=465
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.ssl.enable=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory

我尝试添加到配置文件中,然后再次尝试发送电子邮件。它成功了。我查看了对应的源代码,发现如果我已经在配置文件中填写了用户名和密码,那么 的值不会影响结果。我不明白为什么。spring.mail.properties.mail.smtp.auth=truemail.smtp.auth

protected synchronized boolean protocolConnect(String host, int port, String user, String passwd) throws MessagingException {
        boolean useEhlo = PropUtil.getBooleanSessionProperty(this.session, "mail." + this.name + ".ehlo", true);
        boolean useAuth = PropUtil.getBooleanSessionProperty(this.session, "mail." + this.name + ".auth", false);
        if (this.logger.isLoggable(Level.FINE)) {
            this.logger.fine("useEhlo " + useEhlo + ", useAuth " + useAuth);
        }
        
        if (useAuth && (user == null || passwd == null)) {
            return false;
        } else {
            if (port == -1) {
                port = PropUtil.getIntSessionProperty(this.session, "mail." + this.name + ".port", -1);
            }

            if (port == -1) {
                port = this.defaultPort;
            }

            if (host == null || host.length() == 0) {
                host = "localhost";
            }

            boolean connected = false;

            boolean var9;
            try {
                if (this.serverSocket != null) {
                    this.openServer();
                } else {
                    this.openServer(host, port);
                }

                boolean succeed = false;
                if (useEhlo) {
                    succeed = this.ehlo(this.getLocalHost());
                }

                if (!succeed) {
                    this.helo(this.getLocalHost());
                }

                if (this.useStartTLS || this.requireStartTLS) {
                    if (this.serverSocket instanceof SSLSocket) {
                        this.logger.fine("STARTTLS requested but already using SSL");
                    } else if (this.supportsExtension("STARTTLS")) {
                        this.startTLS();
                        this.ehlo(this.getLocalHost());
                    } else if (this.requireStartTLS) {
                        this.logger.fine("STARTTLS required but not supported");
                        throw new MessagingException("STARTTLS is required but host does not support STARTTLS");
                    }
                }

                if (!useAuth && (user == null || passwd == null) || !this.supportsExtension("AUTH") && !this.supportsExtension("AUTH=LOGIN")) {
                    connected = true;
                    var9 = true;
                    return var9;
                }

                connected = this.authenticate(user, passwd);
                var9 = connected;
            } finally {
                if (!connected) {
                    try {
                        this.closeConnection();
                    } catch (MessagingException var17) {
                    }
                }

            }

            return var9;
        }
    }

我的情况有什么变化?mail.smtp.auth

java spring-boot 电子邮件 jakarta-mail

评论


答:

0赞 long_coder 6/5/2023 #1

它指示 spring 使用基本身份验证。如果没有它,用户名和密码将被忽略。

评论

0赞 pizssn peng 6/6/2023
感谢您的回答,但是当我在配置文件中提供用户名和密码时,是否默认启用了基本身份验证?