目录
- 1.需求分析
- 2.准备工作:开通邮箱的 SMTP 服务
- 3.项目环境搭建
- 4.代码实现
- mail.html
- Employee.java
- MailController.java
- MailService.java
- MailServiceImpl.java
- 5.测试
1.需求分析
采用 Hutool 工具来实现发送邮件的功能,具体来说:为新员工发送一封入职欢迎邮件,在发送邮件时,需要先将新员工的相关信息(例如姓名、职位、职称、部门)设置到 HTML 模板中,然后再进行发送。
Hutool 工具中发送邮件的相关文档见邮件工具-MailUtil。
2.准备工作:开通邮箱的 SMTP 服务
(1)本文以 163 邮箱为例,先注册 163 邮箱,登录 163 邮箱后,开通邮箱的 SMTP 服务,具体步骤如下所示:
需要根据注册时的手机号发送的验证码来开通获取邮箱客户端授权码。开通成功后,会显示个人授权码(如下图所示),该授权码是后面通过 Java 发送邮件的认证密码,非常重要!
3.项目环境搭建
(1)在 IDEA 中创建一个 Spring Boot 项目,具体可以参考【环境搭建】使用IDEA创建SpringBoot项目详细步骤这篇文章。
(2)pom.xml
中添加如下依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency><!-- com.sun.mail 是 javax.mail 升级后的版本,新版本包名做了变更-->
<dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.6.2</version>
</dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.30</version>
</dependency>
(3)在 classpath(在标准 Maven 项目中为 src/main/resources
)的 config 目录下新建 mail.setting
文件,里面设置相关的邮件配置:
# 邮件服务器的 SMTP 地址,可选,默认为 smtp.<发件人邮箱后缀>
host = smtp.163.com
# 邮件服务器的 SMTP 端口,可选,默认 25
port = 25
# 发件人(必须正确,否则发送失败)
from = xxx@163.com
# 用户名,默认为发件人邮箱前缀
user = xxx
# 密码(注意,某些邮箱需要为 SMTP 服务单独设置授权码,此处填上面获取到的授权码)
pass = xxx
注意:除了使用上述配置方式外,我们也可以在 Spring Boot 的配置文件(例如 application.yml)中进行邮件配置,然后在发送邮件时进行读取即可。使用上述方式的好处在于免去了读取这一步骤。
4.代码实现
mail.html
邮件模板 mail.html
如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><head><meta charset="UTF-8"><title>入职欢迎邮件</title></head><body>欢迎 <span th:text="${name}"></span> 加入 XXXX 大家庭,您的入职信息如下:<table border="1"><tr><td>姓名</td><td th:text="${name}"></td></tr><tr><td>职位</td><td th:text="${posName}"></td></tr><tr><td>职称</td><td th:text="${jobLevelName}"></td></tr><tr><td>部门</td><td th:text="${departmentName}"></td></tr></table><p>我们公司的工作忠旨是严格,创新,诚信,您的加入将为我们带来新鲜的血液,带来创新的思维,以及为我们树立良好的公司形象!希望在以后的工作中我们能够齐心协力,与时俱进,团结协作!同时也祝您在本公司,工作愉快,实现自己的人生价值!希望在未来的日子里,携手共进!</p></body>
</html>
Employee.java
员工实体类 Employee.java
如下:
package com.example.mailsend.entity;import lombok.Data;@Data
public class Employee {private String name;private String posName;private String jobLevelName;private String departmentName;private String mail;}
MailController.java
package com.example.mailsend.controller;import com.example.mailsend.entity.Employee;
import com.example.mailsend.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/mail")
public class MailController {@Autowiredprivate MailService mailService;@PostMapping("/send")public String sendMail(@RequestBody Employee employee) {mailService.sendMail(employee);return "success";}
}
MailService.java
package com.example.mailsend.service;import com.example.mailsend.entity.Employee;public interface MailService {void sendMail(Employee employee);}
MailServiceImpl.java
package com.example.mailsend.service.impl;import cn.hutool.extra.mail.MailUtil;
import com.example.mailsend.entity.Employee;
import com.example.mailsend.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;@Slf4j
@Service
public class MailServiceImpl implements MailService {@Autowiredprivate TemplateEngine templateEngine;@Overridepublic void sendMail(Employee employee) {/*** Thymeleaf 模板引擎通过 Context 对象与数据进行绑定,将模板中的表达式与 Context 中的数据关联起来,* 在模板中可以直接引用 Context 中存储的数据,以便动态生成页面内容。* */Context context = new Context();context.setVariable("name", employee.getName());context.setVariable("posName", employee.getPosName());context.setVariable("jobLevelName", employee.getJobLevelName());context.setVariable("departmentName", employee.getDepartmentName());//收件人邮箱String to = employee.getMail();//邮件主题String subject = "欢迎入职";//邮件内容String htmlContent = templateEngine.process("mail", context);//邮件内容是否是 HTML 格式boolean isHtml = true;MailUtil.send(to, subject, htmlContent, isHtml);}
}
5.测试
启动项目后,在 Postman 中进行接口测试(注意是 POST 请求),根并在请求体中设置员工的相关信息(重点是员工邮箱):
http://localhost:8080/mail/send
{"name" : "Tom","posName": "软件开发工程师","jobLevelName" : "高级","departmentName": "软件开发部","mail": "xxx@qq.com"
}
运行成功后会发现员工邮箱已经受到的对应的邮件: