我们现在以QQ邮箱为发件邮箱,模拟给新入职员工发送入职邮件。
工具:IDEA、SpringBoot、QQ邮箱
最详细的流程可参考腾讯官方教程:https://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=371
①先登录发件人的邮箱,来开启POP3/STMP服务,入口如下图:
在账户页面往下翻,然后点击开启POP3/STMP服务,按要求发送短信后,就能拿到一串授权码:
注意:授权码要保密,不然别人知道了你的QQ邮箱和授权码,就能以你的名义乱发邮件了。
②新建SpringBoot项目,只需选Web和Mail
在pom文件中添加Thymeleaf依赖,用来展示邮件内容。
<!--添加Thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
③配置application.properties文件,填你自己的邮箱和刚刚拿到的授权码
spring.mail.host=smtp.qq.com spring.mail.protocol=smtp
#发件人邮箱 spring.mail.username=38AAAAA@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
#这个就是你之前拿到的16个字母的授权码,粘贴到这里 spring.mail.password=ewfgeiajrmkgcajd |
④在测试类中添加测试发送邮件的代码。因为我的项目名称是email,所以测试类叫EmailApplicationTests
如果你自己建立的项目是别的名字,测试类的名称前缀也不同:
下面有2个测试方法,我们选 test1()测试:
package com.zzz.email;
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.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
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.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailApplicationTests {
@Autowired
JavaMailSender javaMailSender;
@Autowired
TemplateEngine templateEngine;
@Test
public void contextLoads() {
SimpleMailMessage msg=new SimpleMailMessage();
msg.setTo("");//发送到
msg.setCc("s38XXXXX@163.com");//抄送到
msg.setSubject("这是测试邮件");//邮件主题
msg.setFrom("38AAAAA@qq.com");//邮件发件人
msg.setSentDate(new Date());//发送时间
msg.setText("这是一封QQ邮件");//邮件内容
javaMailSender.send(msg);
}
@Test
public void test1() throws MessagingException {
MimeMessage mimeMessage=javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage);
helper.setTo("23ZZZZZ@qq.com");//发送到
helper.setCc("s38XXXXX@163.com");//抄送到
helper.setSubject("这是第二封测试邮件");//邮件主题
helper.setFrom("38AAAAA@qq.com");//邮件发件人
helper.setSentDate(new Date());//发送时间
//启用Thymeleaf页面,并设定一些变量,能在email.html页面调用
Context context=new Context();
context.setVariable("username", "李四");
context.setVariable("position", "Java工程师");
context.setVariable("salary","10000" );
//表示是HTML页面,页面名称是email.html
String mail =templateEngine.process("email", context);
helper.setText(mail, true);
javaMailSender.send(mimeMessage);//确认发送
}
}
⑤添加Thymeleaf页面,设置邮件内容展示形式
可见,下面的入职信息:username、position和salary,都是在之前测试类中的Context对象中添加好的,然后在这个页面展示。
<!--看起来是html,其实它是动态资源thymeleaf-->
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>欢迎<span th:text="${username}" style="color: burlywood"></span>加入XXX大家庭!</p>
<div>您的入职信息如下:</div>
<table border="1">
<tr>
<td>职位</td>
<td th:text="${position}"></td>
</tr>
<tr>
<td>薪水</td>
<td th:text="${salary}"></td>
</tr>
</table>
</body>
</html>
⑥选中测试test1()方法,发送邮件:
果然,一个收件人(QQ邮箱)和被抄送人(163邮箱)都收到了邮件:
注意:如果发件人的QQ密码换了,那么他的授权码也必须重新获取,才能发送邮件。