Spring Boot 2.x (十八):郵件服務一文打盡


前景介紹

在日常的工作中,我們經常會用到郵件服務,比如發送驗證碼,找回密碼確認,注冊時郵件驗證等,所以今天在這里進行郵件服務的一些操作。

大致思路

我們要做的其實就是把Java程序作為一個客戶端,然后通過配置SMTP協議去連接我們所使用的發送郵箱(from)對應的SMTP服務器,然后通過SMTP協議,將郵件轉投到目標郵箱(to)對應的SMTP服務器,最后將該郵件分發到目標郵箱

Spring Boot給我們集成了郵件的相關服務,並給出了對應的starter,這里我們來實戰學習一下郵件服務是怎么玩的。

引入POM

萬年不變的第一步:引入所需要的starter依賴,這里我采用的是和我的Spring Boot對應的版本2.1.4,其余版本的話應該是相差不大,可以同樣作為借鑒

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>

配置文件

這里由於國內有幾大郵箱運營商,所以分為四種不同的情況來說明

  • QQ郵箱

    QQ郵箱是比較麻煩的一種,需要登錄到郵箱中找到對應的配置,並驗證密碼后開啟STMP服務

​點擊這里可以去獲取對應的授權碼,后面的配置中我們會用到~

​- 個人QQ郵箱的SMTP服務器的host是:smtp.qq.com

  • 163郵箱

    對應的授權碼就是我們郵箱的密碼~

    SMTP服務器的host是:smtp.163.com

  • 騰訊企業郵箱

    對應的授權碼也是我們郵箱的密碼

    企業的和個人的host略有不同:smtp.exmail.qq.com

  • 阿里企業郵箱

    對應的授權碼也是我們郵箱的密碼

    阿里的企業郵箱host是: smtp.mxhichina.com

得到對應的信息之后,我們就可以去完善我們的配置信息了 ~

# 這里的host對應是上面的幾大運營商的STMP服務器的host
spring.mail.host=smtp.163.com
spring.mail.username=****@163.com
# 這里的password對應的就是上面的授權碼
spring.mail.password=*** 
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.default-encoding=UTF-8

編寫郵件的實體類

/**
 * 郵件實體類
 * @author vi
 * @since 2019/07/17
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Mail {

    /**
     * 郵件發送人
     */
    private String from;
  
    /**
     * 郵件接收人
     */
    private String to;
  
    /**
     * 郵件主題
     */
    private String subject;
  
    /**
     * 郵件內容
     */
    private String content;
  
    /**
     * 郵件主題
     */
    private String type;

    /**
     * 發送郵件模板時的模板文件名
     */
    private String templateName;

    /**
     * 模板參數
     */
    private Map<String,Object> variables;

    /**
     * 附件地址
     */
    private String attachPath;

}

編寫發送郵件的方法

在這里,我將發送郵件分為了兩種情況:

  • 發送普通郵件
   
     /**
       * 發送普通郵件
       * @param email 郵件對象
       */
      private static void sendSimpleMail(Mail email) {
          SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
  //        郵件發送人
          simpleMailMessage.setFrom(email.getFrom());
  //        郵件接收人
          simpleMailMessage.setTo(email.getTo());
  //        郵件主題
          simpleMailMessage.setSubject(email.getSubject());
  //        郵件內容
          simpleMailMessage.setText(email.getContent());
  //        發送郵件
          javaMailSender.send(simpleMailMessage);
      }
      
     
  • 發送MIME類型郵件(比如模板,附件,HTML都屬於該類型的郵件)

  	 /**
       * 發送MIME類型的郵件
       * @param email 郵件對象
       */
      private static void sendMimeMail(Mail email) {
  //        生成郵件字符串
          String content = email.getContent();
          if (email.getVariables() != null) {
              content = generate(email);
          }
  //        基於這個對象可以發送HTML,或者攜帶附件的二進制郵件
          MimeMessage message= javaMailSender.createMimeMessage();
          try {
  //            構建發送模板郵件的對象
              MimeMessageHelper helper = new MimeMessageHelper(message,true);
  //            設置發送郵箱
              helper.setFrom(email.getFrom());
  //            設置接收郵箱
              helper.setTo(email.getTo());
  //            設置郵件名(主題)
              helper.setSubject(email.getSubject());
  //            設置郵件內容
              helper.setText(content,true);
  //            這里可以發送帶有附件的郵件,如果沒有附件可以省略,就不在多做描述
              if (!StringUtils.isNullOrEmpty(email.getAttachPath())) {
                  FileSystemResource file = new FileSystemResource(new File(email.getAttachPath()));
                  helper.addAttachment(file.getFilename(), file);
              }
  //            發送郵件
              javaMailSender.send(message);
          } catch (MessagingException e) {
  
          }
      }
  
  
     /**
       * 生成模板字符串
       * @param email 郵件對象
       * @return
       */
      private static String generate(Mail email) {
          Context context = new Context();
  //        設置模板參數
          context.setVariables(email.getVariables());
  //        加載模板后的內容字符串
          return templateEngine.process(email.getTemplateName(), context);
      }

最后可以把這兩個方法統一接口,通過Mail類中的類型來判斷調用哪一個方法即可~

	/**
     * 對外開放的統一發送郵件方法
     * @param mail
     */
    public static void sendEmail(Mail mail) {
        String  type = mail.getType();
        switch (type) {
            case "1":
                sendSimpleMail(mail);
            case "2":
                sendMimeMail(mail);
        }
    }

關於模板的一些補充

如果我們需要發送模板郵件的話,需要使用到模板引擎freemaker或thymeleaf,這里我拿thymeleaf來說一下~

第一步,可以引入pom文件

<dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

第二步,需要在配置文件中進行配置

spring.thymeleaf.check-template-location=true
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8

第三步,通過我們獲取到的模板參數對Mail類進行set方法

mail.setVariables(email.getVariables());

第四步,我們需要在模板中去使用參數

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
</head>
<body>
<h3 style="color: red;" th:text="${username}"></h3>
</body>
</html>

注意,這里的thymeleaf的用法,使用標簽th:text來賦值,更多的模板用法,可以去

查閱thymeleaf的用法~

公眾號

原創文章,文筆有限,才疏學淺,文中若有不正之處,萬望告知!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM