<!--引入邮件依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--引入Thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
spring:
#发送邮件设置
mail:
host: smtp.163.com
username: [email protected]
#对l63邮箱,password是授权码
password: xxxxxx
properties:
mail:
smtp:
auth: true
starttls:
enable: true
required: true
注意:如果发送方用的是163邮箱,password对应的是邮箱的授权码,而不是你的邮箱密码。
EmailController .java
package com.rabbits.learn.controller;
import com.rabbits.learn.util.JmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.context.Context;
import java.io.File;
/**
* @Description: 发送邮件类
* @Author: RabbitsInTheGrass_xj
* @Date: 2019/10/2 09:39
*/
@RestController
@RequestMapping("/email")
public class EmailController {
@Autowired
private JmsUtil jmsUtil;
/**
* 发送简单的邮件
*
* @return 发送结果
*/
@RequestMapping("sendSimpleEmail")
public String sendSimpleEmail() {
SimpleMailMessage message = new SimpleMailMessage();
// 接收地址
message.setTo("[email protected]");
// 标题
message.setSubject("一封简单的邮件");
// 内容
message.setText("使用Spring Boot发送简单邮件。");
return jmsUtil.sendSimpleEmail(message);
}
}
JmsUtil .java
package com.rabbits.learn.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.internet.MimeMessage;
/**
* @Description: 发送邮件工具类
* @Author: RabbitsInTheGrass_xj
* @Date: 2019/10/2 09:45
*/
@Component
public class JmsUtil {
private static final Logger logger = LoggerFactory.getLogger(JmsUtil.class);
@Autowired
private JavaMailSender jms;
@Autowired
private TemplateEngine templateEngine;
@Value("${spring.mail.username}")
private String from;
public String sendSimpleEmail(SimpleMailMessage message) {
try {
message.setFrom(from);
jms.send(message);
return "发送成功";
} catch (Exception e) {
logger.error(e.getMessage(), e);
return "发送失败";
}
}
}
EmailController .java
/**
* 发送HTML格式内容的邮件
* @return 发送结果
*/
@RequestMapping("sendHtmlEmail")
public String sendHtmlEmail() {
// 接收地址
String toAddress = "[email protected]";
// 标题
String subject = "一封HTML格式内容的邮件";
// 带HTML格式的内容
StringBuilder sb = new StringBuilder("我要发送一份HTML格式内容的邮件。
");
return jmsUtil.sendHtmlEmail(toAddress,subject,sb.toString());
}
JmsUtil .java
public String sendHtmlEmail(String toAddress, String subject, String text) {
try {
MimeMessage message = jms.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddress);
helper.setSubject(subject);
//true表示发送HTML格式邮件
helper.setText(text, true);
jms.send(message);
return "发送成功";
} catch (Exception e) {
logger.error(e.getMessage(), e);
return "发送失败";
}
}
EmailController .java
/**
*发送带附件的邮件
* @return 发送结果
*/
@RequestMapping("sendAttachmentsMail")
public String sendAttachmentsMail() {
// 接收地址
String toAddress = "[email protected]";
// 标题
String subject = "一封带附件的邮件";
// 内容
String text = "详情参见附件内容!";
// 传入附件
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/file/xxxx.doc"));
return jmsUtil.sendAttachmentsMail(toAddress,subject,text,file);
}
JmsUtil .java
public String sendAttachmentsMail(String toAddress, String subject, String text, FileSystemResource file) {
try {
MimeMessage message = jms.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddress);
helper.setSubject(subject);
helper.setText(text);
helper.addAttachment(file.getFilename(), file);
jms.send(message);
return "发送成功";
} catch (Exception e) {
logger.error(e.getMessage(), e);
return "发送失败";
}
}
EmailController .java
/**
* 发送带静态资源的邮件
* @return 发送结果
*/
@RequestMapping("sendInlineMail")
public String sendInlineMail() {
// 接收地址
String toAddress = "[email protected]";
// 标题
String subject = "一封带静态资源的邮件";
String contentId = "img";
// 内容
String text = "一份美图:";
// 传入附件
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/img/yourName.jpg"));
return jmsUtil.sendInlineMail(toAddress,subject,text,file,contentId);
}
JmsUtil .java
public String sendInlineMail(String toAddress, String subject, String text, FileSystemResource file, String contentId) {
MimeMessage message;
try {
message = jms.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(toAddress);
helper.setSubject(subject);
helper.setText(text,true);
//contentId 必须与图片标签里cid后的名称相对应,例如:img
helper.addInline(contentId, file);
jms.send(message);
return "发送成功";
} catch (Exception e) {
logger.error(e.getMessage(), e);
return "发送失败";
}
}
在发送验证码等情况下可以创建一个邮件的模板,唯一的变量为验证码。这个例子中使用的模板解析引擎为Thymeleaf,所以必须引入Thymeleaf依赖。
EmailController .java
/**
* 发送模板邮件
*
* @param code 验证码
* @return 发送结果
*/
@RequestMapping("sendTemplateEmail")
public String sendTemplateEmail(String code) {
//接收地址
String toAddress = "[email protected]";
//标题
String subject = "发送一份模板邮件";
//邮件模板
String templateName = "emailTemplate";
Context context = new Context();
context.setVariable("code", code);
return jmsUtil.sendInlineMail(toAddress, subject, templateName, context);
}
JmsUtil .java
public String sendInlineMail(String toAddress, String subject, String templateName, Context context) {
try {
MimeMessage message = jms.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
// 接收地址
helper.setTo(toAddress);
// 标题
helper.setSubject(subject);
// 处理邮件模板
String template = templateEngine.process(templateName, context);
helper.setText(template, true);
jms.send(message);
return "发送成功";
} catch (Exception e) {
logger.error(e.getMessage(), e);
return "发送失败";
}
}
emailTemplate.html
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8" />
<title>验证码邮件模板</title>
</head>
<body>
您好,您的验证码为<span th:text="${code}"></span>,请在两分钟内使用完成操作。
</body>
</html>
启动服务后请求各个方法即可。