1.登录邮箱 ——>设置
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cf03559d008dca0aab27dae5c28b8dc5.png)
2.开启服务POP3/SMTP——>验证手机号码
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d12631f6d2ed3d36912bcb0f612ffda4.png)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/51d09052d8d40c7138d54bfe6e76bbb9.png)
4.获取授权码保存
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d40eb67948a05fdd01070b36af8f14bd.png)
5.创建项目依赖
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d95dc9d266b1343801b8522df8166737.png)
6.配置依赖.properties
spring.mail.host=smtp.qq.com
spring.mail.protocol=smtp
spring.mail.username=1974627037@qq.com
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
spring.mail.password=hpqxqvacriwibdbe
7.单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
JavaMailSender mailSender;
@Test
public void contextLoads() {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo("2579605371@qq.com");
msg.setCc("757919476@qq.com");
msg.setSubject("这是一封测试邮件");
msg.setFrom("1974627037@qq.com");
msg.setSentDate(new Date());
msg.setText("哈哈!大兄弟行不行");
mailSender.send(msg);
}
8.网页信息发送导pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
9.写个main.html页
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>欢迎<span style="color: #ffff11;" th:text="${username}"></span> 加入
XXX 大家庭</p>
<div>你的入职信息如下:</div>
<table torder="1">
<tr>
<td>职位</td>
<td>th:text="${position}"</td>
</tr>
<tr>
<td>薪资</td>
<td th:text="${salary}"></td>
</tr>
</table>
</body>
</html>
10.单元测试
package org.javaboy.mail;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.Message;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailApplicationTests {
@Autowired
JavaMailSender mailSender;
@Autowired
TemplateEngine templateEngine;
@Test
public void contextLoads() {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setTo("2579605371@qq.com");
msg.setSubject("这是一封测试邮件");
msg.setFrom("1974627037@qq.com");
msg.setSentDate(new Date());
msg.setText("哈哈!大兄弟行不行");
mailSender.send(msg);
}
@Test
public void test1() throws MessagingException{
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
helper.setCc("757919476@qq.com");
helper.setCc("2579605371@qq.com");
helper.setSubject("这是一封测试邮件");
helper.setFrom("1974627037@qq.com");
helper.setSentDate(new Date());
Context context = new Context();
context.setVariable("username","libobo");
context.setVariable("position","总经理");
context.setVariable("salary","10000");
String mail = templateEngine.process("main", context);
helper.setText(mail,true);
mailSender.send(mimeMessage);
}
}