springboot 集成java mail 发邮件时报错:
Caused by: javax.mail.MessagingException: Could not connect to SMTP host: xxxxx, port: 465, response: -1
查询发现发邮件的服务器是https的,
查看javamail的源码发现可以添加 ssl SocketFactory
if (sf == null) {
String sfClass =
props.getProperty(prefix + “.socketFactory.class”);
sf = getSocketFactory(sfClass);
sfErr = “socket factory class ” + sfClass;
}
需要添加如下配置mail.smtp.socketFactory.class: “javax.net.ssl.SSLSocketFactory”
spring
mail:
host:
port: 465
username:
password:
properties: {mail.smtp.socketFactory.class: “javax.net.ssl.SSLSocketFactory”}
//同样可以配置连接、Read超时时间properties: {mail.smtp.socketFactory.class: “javax.net.ssl.SSLSocketFactory”,mail.smtp.connectiontimeout: “10000”, mail.smtp.timeout: “10000”}
default-encoding: UTF-8