1. 引入依赖
在Spring Boot中发送邮件,需要用到spring-boot-starter-mail,引入spring-boot-starter-mail:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
发送模板邮件需要使用模板,引入beetl依赖
<dependency>
<groupId>com.ibeetl</groupId>
<artifactId>beetl-framework-starter</artifactId>
<version>1.1.81.RELEASE</version>
</dependency>
2.邮件配置
server:
port: 8089
servlet:
context-path: /email
spring:
mail:
host: smtp.163.com # 邮箱服务器地址
username: xxx@163.com # 邮箱账号
password: xxxxxx # 授权码
properties:
mail:
smtp:
auth: true
starttls:
enable: true # 个人邮箱设置true
required: true # 个人邮箱设置true
beetl:
suffix: html # beetl模板配置
beetlsql:
enabled: false # 禁用beetlsql
3. 发送简单邮件
@Controller
public class TestController {
@Resource
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String from;
@GetMapping("/senSimpleEmail")
@ResponseBody
public String senSimpleEmail() {
try {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(from);
simpleMailMessage.setTo("505289853@qq.com");//接收地址
simpleMailMessage.setSubject("一封简单的邮件");//标题
simpleMailMessage.setText("使用Spring Boot发送简单邮件");//内容
javaMailSender.send(simpleMailMessage);
} catch (MailException e) {
e.printStackTrace();
}
return "send sucess";
}
}
4. 使用模板发送邮件(这里使用的是beetl模板)
@Controller
public class TestController {
@Resource
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String from;
@GetMapping("senTemplateEmail")
@ResponseBody
public String senTemplateEmail(String code) {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(from);
messageHelper.setTo("505289853@qq.com"); // 接收地址
messageHelper.setSubject("邮件摸板测试"); // 标题
// 处理邮件模板
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("templates");
Configuration cfg = null;
try {
cfg = Configuration.defaultConfiguration();
} catch (IOException e) {
e.printStackTrace();
}
//读取模板内容
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("/emailTemplate.html");
//替换变量
t.binding("code", code);
String template = t.render();
//内容
messageHelper.setText(template, true);//true 表示发送HTML格式邮件
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
return "send success";
}
}
在templates下新建 emailTemplate.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板</title>
</head>
<body>
<div>
<p style="color:red">您好,您的验证码为${code},请在两分钟内使用完成操作。</p>
</div>
</body>
</html>
5. 发送带附件的邮件(在static目录下上传一个附件文件)
@Controller
public class TestController {
@Resource
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String from;
@GetMapping("sendAttachmentsMail")
@ResponseBody
public String sendAttachmentsMail() {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(from);
messageHelper.setTo("505289853@qq.com"); // 接收地址
messageHelper.setSubject("一封带附件的邮件"); // 标题
messageHelper.setText("详情参见附件内容!"); // 内容
//传入附件
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/测试.docx"));
messageHelper.addAttachment("测试aaa.docx", file);//参数一 :发送后显示文件名,
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
return "send success";
}
}
6. 发送带图片的邮件
@Controller
public class TestController {
@Resource
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String from;
/**
* 发送带图片的邮件
* @return
*/
@GetMapping("sendInlineMail")
@ResponseBody
public String sendInlineMail() {
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(from);
messageHelper.setTo("505289853@qq.com"); // 接收地址
messageHelper.setSubject("一封带静态资源的邮件"); // 标题
// 处理邮件模板
ClasspathResourceLoader resourceLoader = new ClasspathResourceLoader("templates");
Configuration cfg = null;
try {
cfg = Configuration.defaultConfiguration();
} catch (IOException e) {
e.printStackTrace();
}
//读取模板内容
GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
Template t = gt.getTemplate("/image.html");
//读取到image.html 内容
String template = t.render();
//内容
messageHelper.setText(template, true);//true 表示发送HTML格式邮件
//传入附件
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image.jpg"));
messageHelper.addInline("img", file);//img和图片标签里cid后的名称相对应
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
return "send success";
}
}
在templates下新建 image.html,并在static新建image.jpg
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div>
图片<img src="cid:img" alt="aaaa">
</div>
</body>
</html>