安照别人的教程走了半天再本地可以但是发布到阿里云就不可以了
大神的教程地址:http://www.ityouknow.com/springboot/2017/05/06/springboot-mail.html
按照地址上的可以完成本地测试,但是发布到阿里云就有很多问题
1. 问题一:
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn’t connect to host, port: smtp.qq.com, 25; timeout -1;
1.1 问题分析:
先来看我们的yml文件配置
spring:
mail:
host: smtp.qq.com
username: [email protected]
default-encoding: UTF-8
password: 您的授权码
-
原来spring boot发邮件默认使用的端口是25,我们在配置中其实并没有配置端口也能运行
-
但是如果本地指定端口或者发布到阿里云服务器,默认的端口25是没有开放的,我们需要设置其它端口
-
如果我们开放一个465端口,也不行,回报错误,如下:
spring: mail: host: smtp.qq.com username: [email protected] default-encoding: UTF-8 password: 您的授权码 port: 465
问题二
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: smtp.qq.com, port: 465, response: -1
2.网上求助
反正楼主网上各种搜索,终于找到了一个解决方案,完美匹配阿里云
方法:
把原来yml配置的东西删掉,重新建一个application.properties文件
其实不新建也行,就是把后面新加的补到yml文件中也行
#email
spring.mail.host=smtp.qq.com
[email protected]
spring.mail.password=您的授权码
spring.mail.properties.mail.smtp.ssl.trust=smtp.qq.com
#SSL证书Socket工厂
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
#使用SMTPS协议465端口
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=tru
问题解决
由于并不是练习项目,而是自己开放的项目,所以不提供项目地址了
有兴趣可以联系楼主:
[email protected]