一、基础知识
邮件常见的三大协议:SMTP、POP3、IMAP
SMTP 是一个基于 TCP/IP 的应用层协议,江湖地位有点类似于 HTTP,SMTP 服务器默认监听的端口号为 25 。由于SMTP 协议是基于 TCP/IP 的应用层协议,我们可以通过 Socket 发送一封邮件。
SMTP 协议全称为 Simple Mail Transfer Protocol,译作简单邮件传输协议,它定义了邮件客户端软件与 SMTP 服务器之间,以及 SMTP 服务器与 SMTP 服务器之间的通信规则。
举个例子:
用户A使用腾讯邮箱,向网易云邮箱的用户发送一封邮件。
A用户先将邮件投递到腾讯的 SMTP 服务器( SMTP 协议),腾讯 SMTP 服务器将邮件投递到网易SMTP 服务器( SMTP 协议,SMTP 服务器就是用来收邮件)。而邮件到了网易的SMTP 服务器,B用户需要登录邮箱,便可以查看邮件(使用POP3协议,POP3 协议全称为 Post Office Protocol ,译作邮局协议,发邮件的)。至于 IMAP 协议,是对 POP3 协议的扩展,功能更强,作用与POP3相似。
二、邮件环境搭建
#pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
#application.properties
#配置邮件
#配置SMTP服务器地址
spring.mail.host=smtp.qq.com
#配置邮箱端口
spring.mail.port=587
#配置邮箱用户名
spring.mail.username=你的邮箱地址
#配置邮箱的授权码
spring.mail.password=邮箱授权码
#配置邮件的编码
spring.mail.default-encoding=UTF-8
#配置ssl加密工厂
mail.properties.mail.smtp.socketFactoryClass=javax.net.ssl.SSLSocketFactory
#开启debug模式,控制台打印邮件发送的日志信息
spring.mail.properties.mail.debug=true
普通邮件测试:
@Autowired
JavaMailSenderImpl mailSender; // 邮件发送
@Test
void mailTest1(){
// 普通邮件发送
SimpleMailMessage message = new SimpleMailMessage(); //存储邮件信息
message.setFrom("@qq.com"); //发送者
message.setTo("@163.com");//接收者
message.setSubject("If you don't ask, you don't get");//设置邮件主题
message.setSentDate(new Date());//设置发送日期
// message.setCc("邮箱地址");//抄送
// message.setBcc("邮箱地址");//隐秘抄送
message.setText("这是一测试人性的邮件,请勿回复");//邮件正文
mailSender.send(message);
}
附件以及图片的邮件发送:
@Autowired
JavaMailSenderImpl mailSender; // 邮件发送
@Test
void mailTest2() throws MessagingException {
// 普通邮件发送
MimeMessage mimeMessage = mailSender.createMimeMessage(); //扩展邮件信息类
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); //添加邮件内容的辅助器
File file = new File("C:\\Users\\lxx\\Desktop\\110.gif");
helper.setFrom("@qq.com"); //发送者
helper.setTo("@qq.com");//接收者
helper.setSubject("If you don't ask, you don't get");//设置邮件主题
helper.setSentDate(new Date());//设置发送日期
// message.setCc("邮箱地址");//抄送
// message.setBcc("邮箱地址");//隐秘抄送
helper.setText("<div>这是一测试人性的邮件,请勿回复</div> <div><image src='cid:p01'></div>",true);//邮件正文
helper.addInline("p01",file);//向指定id添加图片
helper.addAttachment("dog1",file);//添加附件
mailSender.send(mimeMessage);
}
使用thymeleaf作为邮件模板发送邮件
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
简易html模板:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p>信球 欢迎来到河南北部,骄傲的小朋友,先吃碗烩面喝完胡辣汤:</p>
<table border="1" >
<tr>
<td width="100">美食1</td>
<td width="100" th:text="${food1}"></td>
</tr>
<tr>
<td width="100">美食2</td>
<td width="100" th:text="${food2}"></td>
</tr>
<tr>
<td width="100">价格</td>
<td width="100" th:text="${price}"></td>
</tr>
</table>
<div style="color: #37ff4e">生活要想有滋味,头顶绿色不冒昧</div>
</body>
</html>
测试代码:
@Autowired
JavaMailSenderImpl mailSender; // 邮件发送
@Autowired
TemplateEngine templateEngine;//模板引擎 用于生成thymeleaf模板渲染
@Test
void mailTest3() throws MessagingException {
// thymeleaf作模板邮件发送
MimeMessage mimeMessage = mailSender.createMimeMessage(); //扩展邮件信息类
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true); //添加邮件内容的辅助器
File file = new File("C:\\Users\\lxx\\Desktop\\110.gif");
helper.setFrom("1603435095@qq.com"); //发送者
helper.setTo("pianpoguairen@163.com");//接收者
helper.setSubject("If you don't ask, you don't get");//设置邮件主题
helper.setSentDate(new Date());//设置发送日期
Context context = new Context();//创建一个环境,用于设置变量 org.thymeleaf.context.Context;
context.setVariable("food1","胡辣汤");
context.setVariable("food2","烩面");
context.setVariable("price","有朋自远方来,这单免费");
String process = templateEngine.process("mail.html", context);//生成模板 字符串格式
helper.setText(process,true);
helper.addAttachment("dog1.gif",file);//添加附件
mailSender.send(mimeMessage);
}
这是测试用的图片 切勿上升到人格 !!!