1. 程式人生 > >Springboot完美解決在阿里雲釋出郵件的功能

Springboot完美解決在阿里雲釋出郵件的功能

安照別人的教程走了半天再本地可以但是釋出到阿里雲就不可以了

大神的教程地址: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: 您的授權碼
  1. 原來spring boot發郵件預設使用的埠是25,我們在配置中其實並沒有配置埠也能執行

  2. 但是如果本地指定埠或者釋出到阿里雲伺服器,預設的埠25是沒有開放的,我們需要設定其它埠

  3. 如果我們開放一個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]