SpringBoot2.x系列教程69--SpringBoot中整合Mail實現郵件發送

SpringBoot2.x系列教程69--SpringBoot中整合郵件發送

作者:一一哥

注:
本文案例以QQ郵箱發送的實現爲例!

一. 郵件發送概述

1. 概述

在Spring框架中提供了一個JavaMailSender接口,可以實現發送郵件功能。

而在Spring Boot中提供了一個對應的spring-boot-starter-mail依賴,添加該依賴後,Spring Boot將創建一個默認的JavaMailSender,該sender可以通過spring.mail命名空間下的配置項進一步自定義。

2. 發送郵件的場景

  • 用戶通過郵件註冊激活;
  • 通過郵件找回密碼;
  • 通過郵件發送系統情況;
  • 通過郵件發送報表信息等。

3. 常用郵箱系統提供商

126郵箱SMTP服務器地址:smtp.126.com,端口號:465或者994

163郵箱SMTP服務器地址:smtp.163.com,端口號:465或者994

qq郵箱SMTP服務器地址:smtp.qq.com,端口號:465或587

yeah郵箱SMTP服務器地址:smtp.yeah.net,端口號:465或者994

4. QQ郵箱開啓SMTP功能

爲了保障用戶郵箱的安全,QQ郵箱設置了POP3/SMTP/IMAP的開關。系統默認情況下相關設置是“關閉”狀態的,在用戶需要這些功能時請先“開啓”,纔可以用客戶端軟件收發郵件。

QQ郵箱開啓SMTP功能步驟

默認情況下,SMTP服務器功能沒有開啓,所以需要在“設置”-->"賬號"-->"POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務"中對SMTP進行開啓。

可以看到默認情況下並沒有開啓SMTP服務。

點擊開啓按鈕就可以了,但是前提條件是你QQ郵箱綁定了手機號碼,因爲開啓時需要發送短信驗證碼。

開啓成功後,會有一個授權碼,這個授權碼就是我們進行郵件發送時的郵箱密碼,可以把它記住,不記也可以。因爲這個授權碼可以多次生成,只要用的時候發一次短信驗證碼,就可以得到一個新的授權碼了。

關於126或者163郵箱授權碼的獲取過程,與QQ類似,不一一列舉。

二. Spring Boot整合郵件發送實現步驟

1. 創建web項目

我們按照之前的經驗,創建一個web程序,並將之改造成Spring Boot項目,具體過程略。

2. 添加依賴包

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

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

3. 創建application.yml配置文件

spring:
  #freemarker模板配置
  freemarker:
    template-loader-path: classpath:/templates/
   #spring.freemarker.prefix=
    suffix: .ftl
    cache: false
    charset: utf-8
    check-template-location: true
    content-type: text/html
  #郵件配置  
  mail:
    host: smtp.qq.com
    from: [email protected]
    username: [email protected]
    #這裏要替換成自己的郵箱授權碼
    password: xxxxxx
    protocol: smtp
    default-encoding: UTF-8
    #以下可以不配置
    properties:
      mail:
        smtp:
          auth: true
        starttls:
          enable: true
          required: true

QQ郵箱配置

## QQ郵箱配置
spring:
  mail:
    host: smtp.qq.com #發送郵件服務器
    username: [email protected] #發送郵件的郵箱地址
    password:  xxx #客戶端授權碼,不是郵箱密碼,在qq郵箱設置裏面自動生成
    from: [email protected] # 發送郵件的地址,和上面username一致
    protocol: smtp
    default-encoding: UTF-8
    #以下可以配置或者不配置
    properties:
      mail:
        smtp:
          port: 465 #端口號465或587
          auth: true
        starttls:
          enable: true
          required: true

網易(126/163/yeah)郵箱配置

spring:
  mail:
    host: smtp.126.com #發送郵件服務器
    username: [email protected] #發送郵件的郵箱地址
    password: xxxxxxx #客戶端授權碼,不是郵箱密碼,網易的是自己設置的
    properties.mail.smtp.port: 994 #465或者994
    from: [email protected] # 發送郵件的地址,和上面username一致
    default-encoding: UTF-8
    #以下可以配置或者不配置
    properties:
      mail:
        smtp:
          port: 465 #端口號465或994
          auth: true
        starttls:
          enable: true
          required: true

4. 定義發送郵件的服務類

定義郵件發送接口IMailService

package com.yyg.boot.mail;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/20
 * @Description 封裝一個發郵件的接口,方便後邊直接調用.
 */
public interface IMailService {

    /**
     * 發送文本郵件
     *
     * @param to      收件人
     * @param subject 主題
     * @param content 內容
     */
    void sendSimpleMail(String to, String subject, String content);

    /**
     * 發送HTML郵件
     *
     * @param to      收件人
     * @param subject 主題
     * @param content 內容
     */
    void sendHtmlMail(String to, String subject, String content);

    /**
     * 發送帶附件的郵件
     *
     * @param to       收件人
     * @param subject  主題
     * @param content  內容
     * @param filePath 附件
     */
    void sendAttachmentsMail(String to, String subject, String content, String filePath);

    /**
     * 發送模板郵件
     * @param to 收件人
     * @param subject 主題
     * @param fileName 郵件模板文件名稱
     * @param model 郵件數據載體
     */
    void sendModelMail(String to, String subject, String fileName, Object model);

}

定義郵件發送實現類IMailServiceImpl

package com.yyg.boot.mail.impl;

import com.yyg.boot.mail.IMailService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/20
 * @Description Description
 */
@Slf4j
@Service
public class IMailServiceImpl implements IMailService {

    /**
     * Spring Boot 提供了一個發送郵件的簡單抽象,使用的是下面這個接口,這裏直接注入即可使用
     */
    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private Configuration configuration;

    /**
     * 配置文件中我的qq郵箱
     */
    @Value("${spring.mail.from}")
    private String from;

    /**
     * 簡單文本郵件
     *
     * @param to      收件人
     * @param subject 主題
     * @param content 內容
     */
    @Override
    public void sendSimpleMail(String to, String subject, String content) {
        //創建SimpleMailMessage對象
        SimpleMailMessage message = new SimpleMailMessage();
        //郵件發送人
        message.setFrom(from);
        //郵件接收人
        message.setTo(to);
        //郵件主題
        message.setSubject(subject);
        //郵件內容
        message.setText(content);
        //發送郵件
        mailSender.send(message);
    }

    /**
     * html郵件
     *
     * @param to      收件人
     * @param subject 主題
     * @param content 內容
     */
    @Override
    public void sendHtmlMail(String to, String subject, String content) {
        //獲取MimeMessage對象
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper messageHelper;
        try {
            messageHelper = new MimeMessageHelper(message, true);
            //郵件發送人
            messageHelper.setFrom(from);
            //郵件接收人
            messageHelper.setTo(to);
            //郵件主題
            message.setSubject(subject);
            //郵件內容,html格式
            messageHelper.setText(content, true);
            //發送
            mailSender.send(message);
            //日誌信息
            log.info("郵件已經發送...");
        } catch (MessagingException e) {
            log.error("發送郵件時發生異常!", e);
        }
    }

    /**
     * 帶附件的郵件
     * @param to       收件人
     * @param subject  主題
     * @param content  內容
     * @param filePath 附件
     */
    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);

            //FileSystemResource file = new FileSystemResource(new File(filePath));
            ClassPathResource resource = new ClassPathResource(filePath);
            FileSystemResource file = new FileSystemResource(resource.getFile());
            helper.addAttachment(Objects.requireNonNull(file.getFilename()), file);
            //可以同時添加多個附件,只需要在這裏直接添加第2,第3...附件就行了.
            //helper.addAttachment(fileName2, file2);
            mailSender.send(message);
            //日誌信息
            log.info("郵件已經發送...");
        } catch (MessagingException e) {
            log.error("發送郵件時發生異常!", e);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("發送郵件時發生異常!", e);
        }
    }

    @Override
    public void sendModelMail(String to, String subject, String fileName, Object model) {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(from);
            helper.setTo(to);
            helper.setSubject(subject);

            Template template = configuration.getTemplate(fileName);
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);

            helper.setText(html, true);

            mailSender.send(mimeMessage);

            //日誌信息
            log.info("郵件已經發送...");
        } catch (MessagingException e) {
            log.error("發送郵件時發生異常!", e);
        } catch (TemplateException e) {
            e.printStackTrace();
            log.error("發送郵件時發生異常!", e);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

5. 定義實體類

package com.yyg.boot.entity;

import lombok.Data;

import java.util.Date;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/20
 * @Description Description
 */
@Data
public class Employee {

    /**
     * 員工編號
     */
    private Long id;

    /**
     * 員工名稱
     */
    private String username;

    /**
     * 合同期限
     */
    private Integer contractTerm;

    /**
     * 員工薪水
     */
    private Double salary;

    /**
     * 合同起始日期
     */
    private Date beginContract;

    /**
     * 合同截至日期
     */
    private Date endContract;

    /**
     * 部門名稱
     */
    private String departmentName;

    /**
     * 職位名稱
     */
    private String posName;

}

6. 創建入口類

package com.yyg.boot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/20
 * @Description Description
 */
@SpringBootApplication
public class MailApplication {

    public static void main(String[] args){
        SpringApplication.run(MailApplication.class,args);
    }

}

7. 創建Controller接口

7.1 定義發送簡單郵件的接口方法:

@GetMapping("/simple")
public String sendSimpleMail() {
    iMailService.sendSimpleMail("收件箱@qq.com", "郵件標題", "郵件內容.....機密");
    return "success";
}

啓動程序進行測試

郵件發送成功:

去目標郵箱的收件箱中進行查看,可以看到如下郵件內容,說明郵件發送成功!

7.2定義發送html格式郵件的接口方法:

@GetMapping("/html")
public String sendHtmlMail() {
    iMailService.sendHtmlMail("收件箱@qq.com", "郵件主題", "<h1>郵件主題</h1><br/><p><font color='red'>郵件內容</font></p>");
    return "success";
}

郵件發送成功:

去目標郵箱的收件箱中進行查看,可以看到如下郵件內容,說明郵件發送成功!

7.3創建發送帶附件的郵件接口方法:

@GetMapping("/attachment")
public String sendAttachmentMail() {
    iMailService.sendAttachmentsMail("收件箱@qq.com", "主題:帶附件的郵件", "有附件的郵件,不要錯過哦...", "static/touxiang.png");
    return "success";
}

注意:

我這裏是把附件直接放到了項目的resource/static目錄下了,我們也可以存放到桌面等位置。

郵件發送成功:

然後去目標郵箱的收件箱中進行查看,可以看到如下郵件內容,說明郵件發送成功!

7.4創建發送模板郵件的接口方法:

首先利用FreeMarker創建頁面模板。

<p>${username}--你好,歡迎加入XXX大家庭!您的入職信息如下:</p>
<table border="1" cellspacing="0">
    <tr><td><strong style="color: #F00">工號</strong></td><td>${id}</td></tr>
    <tr><td><strong style="color: #F00">合同期限</strong></td><td>${contractTerm}年</td></tr>
    <tr><td><strong style="color: #F00">員工薪資</strong></td><td>${salary}/月(美元)</td></tr>
    <tr><td><strong style="color: #F00">合同起始日期</strong></td><td>${beginContract?string("yyyy-MM-dd")}</td></tr>
    <tr><td><strong style="color: #F00">合同截至日期</strong></td><td>${endContract?string("yyyy-MM-dd")}</td></tr>
    <tr><td><strong style="color: #F00">所屬部門</strong></td><td>${departmentName}</td></tr>
    <tr><td><strong style="color: #F00">職位</strong></td><td>${posName}</td></tr>
</table>
<p><strong style="color: #F00; font-size: 24px;">希望在未來的日子裏,攜手共進!</strong></p>

別忘了在application.yml文件中對freemarker進行配置:

spring:
  freemarker:
    template-loader-path: classpath:/templates/
   #spring.freemarker.prefix=
    suffix: .ftl
    cache: false
    charset: utf-8
    check-template-location: true
    content-type: text/html

創建接口方法

 @PostMapping("/model")
 public String sendModelMail(@RequestBody Employee employee) {
    iMailService.sendModelMail("收件箱@qq.com", "主題:新員工入職歡迎郵件--模板郵件", "mail.ftl", employee);
    return "success";
}

在postman中進行接口測試

然後去目標郵箱的收件箱中進行查看,可以看到如下郵件內容,說明郵件發送成功!

8. Controller完整代碼

package com.yyg.boot.web;

import com.yyg.boot.entity.Employee;
import com.yyg.boot.mail.IMailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 一一哥Sun
 * @Date Created in 2020/4/20
 * @Description Description
 */
@RestController
@RequestMapping("/mail")
public class MailController {

    @Autowired
    private IMailService iMailService;

    @GetMapping("/simple")
    public String sendSimpleMail() {
        iMailService.sendSimpleMail("[email protected]", "郵件標題", "郵件內容.....機密");
        return "success";
    }

    @GetMapping("/html")
    public String sendHtmlMail() {
        iMailService.sendHtmlMail("[email protected]", "郵件主題", "<h1>郵件主題</h1><br/><p><font color='red'>郵件內容</font></p>");
        return "success";
    }

    @GetMapping("/attachment")
    public String sendAttachmentMail() {
        iMailService.sendAttachmentsMail("[email protected]", "主題:帶附件的郵件", "有附件的郵件,不要錯過哦...", "static/touxiang.png");
        return "success";
    }

    @PostMapping("/model")
    public String sendModelMail(@RequestBody Employee employee) {
        iMailService.sendModelMail("[email protected]", "主題:新員工入職歡迎郵件--模板郵件", "mail.ftl", employee);
        return "success";
    }

}

9. 完整項目結構




 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章