pizssn peng
pizssn peng

Reputation: 21

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

I use JavaMailSender to send emails. I configured the corresponding username and password in the configuration file, but an error occurred when sending the email. The error message is org.apache.geronimo.javamail.transport.smtp.SMTPSendFailedException: authentication is required.

Here is my configuration file

# 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

I tried adding spring.mail.properties.mail.smtp.auth=true to the configuration file and then tried sending the email again. It was successful. I looked at the corresponding source code and found that if I had already filled in the username and password in the configuration file, the value of mail.smtp.auth would not affect the result. I don’t understand why.

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

What does mail.smtp.auth change in my case?

Upvotes: 1

Views: 2060

Answers (1)

long_coder
long_coder

Reputation: 43

It instructs spring to use basic authentication. Without it the username and password would be ignored.

Upvotes: 0

Related Questions