最近做一个邮件发送功能的时候,发现腾讯企业邮的邮箱,用java发送邮件的时候一直报错:
Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 465, response: -1. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.exmail.qq.com, port: 465, response: -1

发邮件使用的spring-boot-starter-mail,配置文件如下:

# 设置邮箱主机
spring.mail.host=smtp.exmail.qq.com
spring.mail.port=465
spring.mail.protocol=smtp

# 设置用户名
spring.mail.username=xxxxxx
# 设置密码,是客户端专用密码,非网页登录密码
spring.mail.password=xxxxxx

# true代表邮箱需要认证
spring.mail.properties.mail.smtp.auth=true

#启用SSL
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

查阅各种资料后发现,配置文件应当加入下面的配置:

spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.port=465
spring.mail.properties.mail.smtp.socketFactory.port=465

将这三行加入到配置文件里就可以正常发送邮件了