其他
Spring Boot 2 发送邮件手把手图文教程
The following article is from IT牧场 Author itmuch
最近有童鞋问到笔者如何用Spring Boot发送邮件,故而整理下Spring Boot发送邮件的各种姿势。
JavaMailSender
都不陌生。那么Spring Boot是否有开箱即用的邮件发送呢?spring-boot-starter-mail
。一、邮箱配置
二、编码
2.1 准备工作
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring:
mail:
host: smtp.126.com
username: eacdy0000@126.com
password: 上面设置的授权码
2.2 发送简单邮件
public String simple() {
SimpleMailMessage message = new SimpleMailMessage();
// 发件人邮箱
message.setFrom(this.mailProperties.getUsername());
// 收信人邮箱
message.setTo("511932633@qq.com");
// 邮件主题
message.setSubject("简单邮件测试");
// 邮件内容
message.setText("简单邮件测试");
this.javaMailSender.send(message);
return "success";
}
2.3 发送HTML邮件
public String html() throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(message);
messageHelper.setFrom(this.mailProperties.getUsername());
messageHelper.setTo("511932633@qq.com");
messageHelper.setSubject("HTML内容邮件测试");
// 第二个参数表示是否html,设为true
messageHelper.setText("<h1>HTML内容..</h1>", true);
this.javaMailSender.send(message);
return "success";
}
2.4 发送带附件的邮件
@GetMapping("/attach")
public String attach() throws MessagingException {
MimeMessage message = this.javaMailSender.createMimeMessage();
// 第二个参数表示是否开启multipart模式
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(this.mailProperties.getUsername());
messageHelper.setTo("511932633@qq.com");
messageHelper.setSubject("带附件的邮件测试");
// 第二个参数表示是否html,设为true
messageHelper.setText("<h1>HTML内容..</h1>", true);
messageHelper.addAttachment("附件名称",
new ClassPathResource("wx.jpg"));
this.javaMailSender.send(message);
return "success";
}
2.5 发送带内联附件的邮件
@GetMapping("/inline-attach")
public String inlineAttach() throws MessagingException {
MimeMessage message = this.javaMailSender.createMimeMessage();
// 第二个参数表示是否开启multipart模式
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(this.mailProperties.getUsername());
messageHelper.setTo("511932633@qq.com");
messageHelper.setSubject("内联附件的邮件测试");
// 第二个参数表示是否html,设为true
messageHelper.setText("<h1>HTML内容..<img src=\"cid:attach\"/></h1>", true);
messageHelper.addInline("attach", new ClassPathResource("wx.jpg"));
this.javaMailSender.send(message);
return "success";
}
cid:xx
引用内联附件,然后用 addInline(xx, file)
指定附件即可。两处的 xx
必须一致。2.6 发送基于Freemarker模板的邮件
mail.ftl
,并将其存放在resources/templates/
目录中<h1>亲爱的${username}, 欢迎关注${event}</h1>
@GetMapping("/freemarker")
public String freemarker() throws MessagingException, IOException, TemplateException {
MimeMessage message = this.javaMailSender.createMimeMessage();
// 第二个参数表示是否开启multipart模式
MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
messageHelper.setFrom(this.mailProperties.getUsername());
messageHelper.setTo("511932633@qq.com");
messageHelper.setSubject("基于freemarker模板的邮件测试");
Map<String, Object> model = new HashMap<>();
model.put("username", "itmuch");
model.put("event", "IT牧场大事件");
String content = FreeMarkerTemplateUtils.processTemplateIntoString(
this.freemarkerConfiguration.getTemplate("mail.ftl"), model);
// 第二个参数表示是否html,设为true
messageHelper.setText(content, true);
this.javaMailSender.send(message);
return "success";
}
References
[1]
GitHub: https://github.com/eacdy/spring-boot-study/tree/master/spring-boot-mail[2]
Gitee: https://gitee.com/itmuch/spring-boot-study/tree/master/spring-boot-mail