使用JavaMailSender发送邮件报:SMTPSendFailedException: authentication is required

文章描述了一次由于服务器到期和报警邮件失效导致的SMTP邮件发送失败的问题。作者在排查过程中发现是缺少mail.smtp.auth=true的配置,添加后解决了认证问题。尽管测试环境无需此配置也能工作,但在生产环境中是必需的。作者还分享了完整的邮件配置示例,并指出即使不设置mail.smtp.auth,只要提供了用户名和授权码,邮件发送仍应正常进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

起因是发提醒邮件的定时任务所在的服务器到期不用了,我给忘记有这个定时任务,报警邮件也失效了,导致该定时任务失败。后续迁移服务的时候就出现了这个问题,一切配置如常,但一直报错Failed messages: org.apache.geronimo.javamail.transport.smtp.SMTPSendFailedException: authentication is required,163 zwqz-smtp-mta-g3-3

一、排查问题

  我先去测试环境用相同的配置试了一下,可以正常发送邮件也不会报错。
  于是我猜测是生产环境的mailSender没有获取到正确的授权码导致的认证问题。但是debug的时候,发现mailSender的信息全都正确获取。而且我这个password填写的就是认证码,并不是邮箱密码。
!](https://img-blog.csdnimg.cn/c2fdbfc9afc7462ab5b61d20c80070e3.png)

  接着我又排查了jar包等依赖问题,发现测试环境和生产环境使用的相应依赖均一致。
  我还检查了我使用的相应配置文件有没有问题,发现和测试环境一样,但就是生产环境发邮件报错,测试环境就没问题。
  后面浏览相应的博客java使用smtp协议发送邮件注意的问题,发现了一个新的配置选项,而且在此博客上还对于相应的配置有一定的解释。

二、解决问题

  这个配置就是props.put("mail.smtp.auth", "true");
如果使用的是配置文件进行配置,则为

spring.mail.properties.mail.smtp.auth=true

完整的邮箱配置为

# 邮件配置
management.health.mail.enabled=false
#配置POP3服务器,此处使用网易邮箱
spring.mail.host=smtp.163.com
#发送邮件的用户名及授权码,注意不是登陆密码
spring.mail.username=yy13*****1@163.com
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.auth=true
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

三、后续产生的一些思考

  一开始测试环境和原生产环境的那个服务器我是没有配置mail.smtp.auth=true的,但是他们都是可以正常发送邮件。但是现生产环境就不行,我一开始还以为是因为现生产环境是存在vpn阻隔导致的。
  但是看了源码发现,如果我配置相应的username和password,mail.smtp.auth=true是不起作用的。

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

  可以看到,其实只要你再配置文件配置了相应的username和password,mail.smtp.auth默认为false,也是不影响的,因为mail.smtp.auth的判断条件里往往还有(user == null || passwd == null)。因此这个地方还是存在疑点的。后续有时间再跟一下源码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值