系统最近从测试环境部署到正式环境,之前在springboot配置的java mail配置工作都是正常的,但是迁移到了生产环境不工作了。因为之前没有在配置端口,默认是25。生产环境用的是465端口,用到了ssl认证。控制台报错信息如下:
1 | org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not convert socket to TLS; |
经查询,原因应该是没有通过邮件服务器认证造成的,还需要在配置文件中加上spring.mail.properties.mail.smtp.ssl.trust
的配置
最后,附上springboot mail相关的配置:1
2
3
4
5
6
7
8
9
10
11
12
13#Email service
spring.mail.host=smtp.xxx.com
spring.mail[email protected]
spring.mail.password=xxxxxx
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=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.ssl.trust=smtp.xxx.com
spring.mail.properties.mail.smtp.connectiontimeout=30000
spring.mail.properties.mail.smtp.timeout=20000
spring.mail.properties.mail.smtp.writetimeout=10000