在Spring Boot項目中,使用org.springframework.mail.javamail.JavaMailSender發送郵件拋異常,異常信息:
Mail server connection failed; nested exception is javax.mail.MessagingException: Got bad greeting from SMTP host: smtp.163.com, port: 465,
response: [EOF]. Failed messages: javax.mail.MessagingException: Got bad greeting from SMTP host: smtp.163.com, port: 465, response: [EOF]
問題分析:設置郵件協議的時候,配置如下:
spring.mail.protocol=smtp
#設置ssl端口
spring.mail.port=465
注意,是smtp,結尾沒有字母s。另外,在不設置該屬性的時候,系統默認使用SMTP協議。
解決方案:修改配置文件,使用郵件傳輸協議SMTPS,使得傳輸協議與端口465相匹配:
spring.mail.protocol=smtps
結論:重啟服務,經驗證問題搞定。其實,網上很多人寫的博客都把配置信息寫錯了,我就是抄錯了,這里記下來,亡羊補牢。