起因是发提醒邮件的定时任务所在的服务器到期不用了,我给忘记有这个定时任务,报警邮件也失效了,导致该定时任务失败。后续迁移服务的时候就出现了这个问题,一切配置如常,但一直报错Failed messages: org.apache.geronimo.javamail.transport.smtp.SMTPSendFailedException: authentication is required,163 zwqz-smtp-mta-g3-3
一、排查问题
我先去测试环境用相同的配置试了一下,可以正常发送邮件也不会报错。
于是我猜测是生产环境的mailSender没有获取到正确的授权码导致的认证问题。但是debug的时候,发现mailSender的信息全都正确获取。而且我这个password填写的就是认证码,并不是邮箱密码。
接着我又排查了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)
。因此这个地方还是存在疑点的。后续有时间再跟一下源码。