实现邮件发送功能步骤:
建立springboot项目 ---> 申请qq邮箱授权码 ---> 配置文件 ---> 代码实现
目录
一、新建springboot服务
添加依赖包:
<!-- mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- 邮件模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、申请qq邮箱授权码
申请方法:QQ邮箱->设置->账户->POP3/SMTP服务->开启;开启服务后发送短信验证会获得QQ的授权码。
三、application.yml 文件配置
spring:
mail:
host: smtp.qq.com
password: #qq授权码
username: #自己的qq账号
default-encoding: utf-8
四、实现
Java代码:
@Service
public class MailService {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender sender;
//发送简单邮件
public void sendMail(String sendTo,String subject,String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(subject);
message.setTo(sendTo);
message.setText(content);
message.setFrom(from);
sender.send(message);
}
//发送简单html邮件
public void sendHtmlMail(String sendTo,String subject,String content) throws MessagingException {
MimeMessage message = sender.createMimeMessage();
//封装邮件格式内容
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content,true);
sender.send(message);
}
//发送附件邮件,多附件
public void sendAttachMail(String sendTo, String subject, String content, List<String> filePathList) throws MessagingException {
MimeMessage message = sender.createMimeMessage();
//封装邮件格式内容
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content,true);
//附件
for (int i = 0; i < filePathList.size(); i++) {
FileSystemResource resource = new FileSystemResource(new File(filePathList.get(i)));
//获取文件名
String fileName = resource.getFilename();
//添加附件
helper.addAttachment(fileName,resource);
}
sender.send(message);
}
}
html模板:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮件模板</title>
</head>
<body>
这是一个验证邮件,点击下面的链接注册
<a href="#" th:href="@{http://www.baidu.com/{id}(id=${id})}">激活账号</a>
</body>
</html>
五、单元测试
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)//springboot启动类
public class MailServiceTest {
@Resource
MailService service;
@Resource
TemplateEngine templateEngine;
//发送简单邮件
@Test
public void sendMail() {
service.sendMail("接收方的邮箱地址","邮件标题","邮件内容");
}
//发送多附件邮件
@Test
public void sendAttachMail() throws MessagingException {
List<String> fileList = new ArrayList<>();
fileList.add("你的附件地址1");
fileList.add("你的附件地址2");
StringBuffer sb = new StringBuffer();
sb.append("<h1>我的多附件邮件</h1>");
sb.append("<p>附件1</p>");
sb.append("<p>附件2</p>");
service.sendAttachMail("接收方的邮箱地址","我的第一封附件邮件。。",sb.toString(),fileList);
}
//通过邮件模板发送邮件
@Test
public void testTemplates() throws MessagingException {
Context context = new Context();
context.setVariable("id","220");
String content = templateEngine.process("emailTemplate",context);
service.sendHtmlMail("接收方的邮箱地址","模板邮件22",content);
}
}