天天看點

springboot中使用javamail javaMail的使用

javaMail的使用

  1. 先來展示下收到郵件的效果圖:
    springboot中使用javamail javaMail的使用
  2. 準備工作
    • 郵箱伺服器位址:這裡使用新浪郵箱,登入你的新浪郵箱,開啟SMTP
    • 目标郵箱位址:可以是自己的qq郵箱,能接到郵件就行
  3. maven引入依賴包,thymeleaf有提供htmlmail模闆
    <!--mail-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-mail</artifactId>
       </dependency>
    
       <!--thymeleaf-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-thymeleaf</artifactId>
       </dependency>
               
  4. 配置相關資訊
    spring:  
     thymeleaf:
       cache: false
       mode: LEGACYHTML5
     mail:
         # 郵箱設定裡可以找到
         host: smtp.sina.com
         # 去頁面開啟SMTP
         username:
         password:
         default-encoding: UTF-8
               
  5. 編寫相關代碼

繼續閱讀