服务器操作系统是 CentOS Linux release 7.9.2009 (Core)
spring-boot-starter-mail:2.0.4
文中的日志都经过了脱敏处理,日志中的 bbb.ccc 指代域名,非真实存在。
jdk version “1.8.0_351”
这是一个hostname名称引发的邮件发送失败案例。起因是我想部署一套开源BI软件Davinci。Davinci 注册用户需要用邮件验证,必须要配置好邮件服务。配置文档链接 公司使用的是阿里云的企业邮箱。找了个邮箱开始配置。核心配置如下
spring:
mail:
host: smtp.bbb.ccc
port: 25
username: aaa@bbb.ccc
fromAddress: aaa@bbb.ccc
password: 'mypassword'
nickname: Davinci
properties:
mail:
debug: true
- 这个配置走的是没有SSL加密的25端口,没有问题本地一次通过,日志如下
DEBUG: JavaMail version 1.6.1
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.bbb.ccc", port 25, isSSL false
220 smtp.aliyun-inc.com MX AliMail Server
DEBUG SMTP: connected to host "smtp.bbb.ccc", port: 25
EHLO DESKTOP-5F0JMOF
250-smtp.aliyun-inc.com
250-STARTTLS
250-8BITMIME
250-AUTH=PLAIN LOGIN XOAUTH2 XALIOAUTH
250-AUTH PLAIN LOGIN XOAUTH2 XALIOAUTH
250-PIPELINING
250 DSN
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN XOAUTH2 XALIOAUTH"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XOAUTH2 XALIOAUTH"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.bbb.ccc, user=aaa@bbb.ccc, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<aaa@bbb.ccc>
250 Mail Ok
RCPT TO:<haohaifeng@bbb.ccc>
250 Rcpt Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: haohaifeng@bbb.ccc
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Fri, 18 Jul 2025 11:32:42 +0800 (CST)
From: "aaa@bbb.ccc" <aaa@bbb.ccc>
To: haohaifeng@bbb.ccc
Message-ID: <1336710910.0.1752809562571@DESKTOP-5F0JMOF>
Subject: aaa
MIME-Version: 1.0
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: base64
6L+Z5piv5LiA5Lu95rWL6K+V6YKu5Lu2
.
250 Data Ok: queued as freedom
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 Bye
spring:
mail:
host: smtp.bbb.ccc
port: 465
username: aaa@bbb.ccc
fromAddress: aaa@bbb.ccc
password: 'mypassword'
nickname: Davinci
properties:
mail:
debug: true
smtp:
auth: true
ssl:
enable: true
trust: smtp.bbb.ccc
starttls:
enable: false
- 这个配置走的SSL加密的465端口,没有问题本地一次通过,日志如下
DEBUG: JavaMail version 1.6.1
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.bbb.ccc", port 465, isSSL true
220 smtp.aliyun-inc.com MX AliMail Server
DEBUG SMTP: connected to host "smtp.bbb.ccc", port: 465
EHLO DESKTOP-5F0JMOF
250-SMTP.ALIYUN-INC.COM
250-8BITMIME
250-AUTH=PLAIN LOGIN XOAUTH2 XALIOAUTH
250-AUTH LOGIN
250 DSN
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN XOAUTH2 XALIOAUTH"
DEBUG SMTP: Found extension "AUTH", arg "LOGIN"
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.bbb.ccc, user=aaa@bbb.ccc, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<aaa@bbb.ccc>
250 Mail Ok
RCPT TO:<haohaifeng@bbb.ccc>
250 Rcpt Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: haohaifeng@bbb.ccc
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Fri, 18 Jul 2025 11:49:02 +0800 (CST)
From: "aaa@bbb.ccc" <aaa@bbb.ccc>
To: haohaifeng@bbb.ccc
Message-ID: <2062716602.0.1752810542378@DESKTOP-5F0JMOF>
Subject: aaa
MIME-Version: 1.0
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: base64
6L+Z5piv5LiA5Lu95rWL6K+V6YKu5Lu2
.
250 Data Ok: queued as freedom
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 smtp.aliyun-inc.com Service closing transmission channel
- 但是部署到服务器上两种方式都不行
方式一日志:
方式二日志:
## 注意这个日志
EHLO jhmd-test-6-152
DEBUG SMTP: EOF: [EOF]
HELO jhmd-test-6-152
DEBUG SMTP: EOF: [EOF]
两种配置都是在连接 SMTP 服务器时,在 EHLO 或 HELO 命令后,服务器直接返回 [EOF],连接被关闭。
这个时候第一反应是服务器有问题。
# telnet smtp.bbb.ccc 465
# telnet smtp.bbb.ccc 25
测试端口没问题,465端口SSL证书搞一遍
# echo | openssl s_client -connect smtp.bbb.ccc:465 | openssl x509 -outform PEM > smtp.bbb.ccc.pem
## 验证证书
# openssl s_client -connect smtp.bbb.ccc:465 -tls1_2
## 证书导入jdk
# keytool -importcert -alias smtp.bbb.ccc\
-keystore $JAVA_HOME/lib/security/cacerts \
-file smtp.bbb.ccc.pem
## 默认密码是 changeit,导入后重启
## JDK1.8如果没有$JAVA_HOME/lib/security就从$JAVA_HOME/jre/lib/security拷贝一份再操作
还是不行
安装mailx发个邮件试试看,先排除程序问题
## 安装
# yum install -y mailx
## 服务配置
# vim /etc/mail.rc
# SMTP配置
set smtp=smtps://smtp.bbb.ccc:465
set smtp-auth=login
set smtp-auth-user=aaa@bbb.ccc
set smtp-auth-password='mypassword'
set from=aaa@bbb.ccc
set nss-config-dir=/etc/pki/nssdb
set ssl-verify=ignore
## 发送邮件命令
# echo "这是一封测试邮件,请忽略" | mailx -v -s "test mail" haohaifeng@bbb.ccc

邮件发送正常
难道真是程序问题,可本地为什么能发出去呢?
没招了,问问AI吧,给个思路。先感谢kimi K2
解决步骤中针对第三点kimi给了详细的解决方案
看看hostname和hosts,结合之前的日志。有可能是这个原因
再 配置上试试吧:
spring:
mail:
host: smtp.bbb.ccc
port: 465
username: aaa@bbb.ccc
fromAddress: aaa@bbb.ccc
password: 'mypassword'
nickname: Davinci
properties:
mail:
debug: true
smtp:
auth: true
ssl:
enable: true
trust: smtp.bbb.ccc
starttls:
enable: false
localhost: bbb.ccc# 👈 关键配置
DEBUG: JavaMail version 1.6.1
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.bbb.ccc", port 465, isSSL true
220 smtp.aliyun-inc.com MX AliMail Server
DEBUG SMTP: connected to host "smtp.bbb.ccc", port: 465
EHLO bbb.ccc
250-smtp.aliyun-inc.com
250-8BITMIME
250-AUTH=PLAIN LOGIN XOAUTH2 XALIOAUTH
250-AUTH PLAIN LOGIN XOAUTH2 XALIOAUTH
250-PIPELINING
250 DSN
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN XOAUTH2 XALIOAUTH"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN XOAUTH2 XALIOAUTH"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: protocolConnect login, host=smtp.bbb.ccc, user=aaa@bbb.ccc, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
DEBUG SMTP: Using mechanism LOGIN
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN succeeded
DEBUG SMTP: use8bit false
MAIL FROM:<aaa@bbb.ccc>
250 Mail Ok
RCPT TO:<haohaifeng@bbb.ccc>
250 Rcpt Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: haohaifeng@bbb.ccc
DATA
354 End data with <CR><LF>.<CR><LF>
Date: Fri, 18 Jul 2025 13:55:45 +0800 (CST)
From: "aaa@bbb.ccc" <aaa@bbb.ccc>
To: haohaifeng@bbb.ccc
Message-ID: <1047177916.0.1752818185352@jhmd-test-6-152>
Subject: aaa
MIME-Version: 1.0
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: base64
6L+Z5piv5LiA5Lu95rWL6K+V6YKu5Lu2
.
250 Data Ok: queued as freedom
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 Bye
Well Done! 终于可以发邮件了。浪费时间啊









被折叠的 条评论
为什么被折叠?



