Spring Boot 2发送邮件手把手图文教程
原文:http://www.itmuch.com/spring-boot/send-email/
本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本。
最近有童鞋问到笔者如何用Spring Boot发送邮件,故而整理下Spring Boot发送邮件的各种姿势。
说到邮件放松,相信大家对Spring Framework提供的接口 JavaMailSender 都不陌生。那么Spring Boot是否有开箱即用的邮件发送呢?
答案是肯定的。Spring Boot为发送邮件提供了starter:spring-boot-starter-mail 。
本文详细探讨如何用Spring Boot发送邮件。
一、邮箱配置
以126邮箱为例:
开启SMTP服务

设置/重置客户端授权密码

二、编码
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邮件
简单邮件是没有样式的,很多时候,我们希望发送的邮件内容带有样式,此时可发送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 发送带内联附件的邮件
附件 + HTML基本能满足日常工作中多数需求。但如果能将附件内联在邮件内容中,那么体验就更好啦!如何实现附件的内联呢?
@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模板的邮件
上面的例子中,邮件内容是直接以字符串体现的,这通常不适合生产,因为实际项目中邮件往往带有变量。此时,可考虑使用Freemarker模板(或者其他模板,Spring Boot 2.x默认支持Freemarker、Groovy、Thymeleaf、Mustache四种模板引擎,也可根据需求使用其他模板引擎)。
创建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";
}
此时,结果类似下图:

三、配套代码
Spring Boot 2发送邮件手把手图文教程的更多相关文章
- Spring Boot 2.0 配置图文教程
摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! 本章内容 自定义属性快速入门 外化配置 自动配置 自定义创建 ...
- Spring Boot 单元测试详解+实战教程
Spring Boot 的测试类库 Spring Boot 提供了许多实用工具和注解来帮助测试应用程序,主要包括以下两个模块. spring-boot-test:支持测试的核心内容. spring-b ...
- Spring Boot 之发送邮件
Spring Boot 之发送邮件 简介 API 配置 实战 引入依赖 配置邮件属性 Java 代码 完整示例 引申和引用 简介 Spring Boot 收发邮件最简便方式是通过 spring-boo ...
- Spring Boot (17) 发送邮件
添加依赖 <!--发送邮件 --> <dependency> <groupId>org.springframework.boot</groupId> & ...
- 基于Spring boot的web项目搭建教程(一)
前言: 本教程参考了大量前辈的代码,在此不方便一一列举.本教程使用IDEA开发工具搭建项目,对于本人的IDEA已经集成了某些插件,比如Lombok,Thymeleaf,yml等插件,这些插件不在文中提 ...
- Spring Boot 缓存应用 Memcached 入门教程
本章学习 Mmecached 在 Spring Boot 中的使用教程.Memcached 与 Redis 各有好处.本文主要学习 Spring Boot 中如何应用集成 Mmecached spri ...
- Spring Boot 2.0.1 入门教程
简介 Spring Boot是Spring提供的一套基础配置环境,可以用来快速开发生产环境级别的产品.尤其适合开发微服务架构,省去了不少配置麻烦.比如用到Spring MVC时,只需把spring-b ...
- Spring Boot gradle 集成servlet/jsp 教程及示例
1.build.gradle 配置 注意,加入了war插件,在依赖中加入了jstl.tomcat-embed-jasper,这样才能运行jsp页面. buildscript { ext { sprin ...
- Spring Boot 文件上传简易教程
上传文件是我们日常使用最为广泛的功能之一,比如App端上传头像.本章演示如何从客户端上传到 Spring Boot 开发的 Api中. 本项目源码 github 下载 1 新建 Spring Boot ...
随机推荐
- 解决HTM或HTML的图标
HTM和HTML的文件图标不能正常显示,显示为无关联应用程序的白板图标,搞了很久都没能解决,最后综合了几种方法才“搞定”她!出现这种情况的原因可能是安装了某些软件(比如OFFICE.FIREFOX)后 ...
- 【VS开发】IP地址格式转换(htonl、ntohl;inet_addr、inet_ntoa)
1.htonl ()和ntohl( ) u_long PASCAL FAR ntohl (u_long netlong); u_short PASCAL FAR ntohs (u_short nets ...
- 修正线性单元(Rectified linear unit,ReLU)
修正线性单元(Rectified linear unit,ReLU) Rectified linear unit 在神经网络中,常用到的激活函数有sigmoid函数f(x)=11+exp(−x).双曲 ...
- JMeter 脚本请求错误 HTTP Status 415 的解决
然后在http请求上点击右键,添加配置元件-http信息头管理器,在信息头管理器上,添加一个参数,名称:Content-Type,值:application/json.然后在http请求上,conte ...
- servlet02
内容 1servlet 2HTTP协议 3Request servlet继承的体系结构 抽象类 | GenericServlet:将servlet接口中其他方法默认空实现,只将servic ...
- LeetCode 172. 阶乘后的零(Factorial Trailing Zeroes)
172. 阶乘后的零 172. Factorial Trailing Zeroes 题目描述 给定一个整数 n,返回 n! 结果尾数中零的数量. LeetCode172. Factorial Trai ...
- [转帖]Red Hat K8s 关键人物 Grant Shipley 跳槽到 VMware
Red Hat K8s 关键人物 Grant Shipley 跳槽到 VMware https://news.cnblogs.com/n/641944/ 这四小时的工作效率 太无敌了.. 投递人 ...
- [Xamarin] - Xamarin.Forms Project with .Net Standard 2.0
1. Install .NET Core 2.0 SDK .https://www.microsoft.com/net/download/core 2. Install Android 7.1 (AP ...
- 定时任务-C#线程类 windows服务
原理 最常用的就是C#中 timer类写一个定时方法,然后在把他宿主到windows服务里面. C#中Timer分类 关于C# Timer类 在C#里关于定时器类就有3个 C# Timer使用的方法 ...
- SecureCRT系列:生成公私钥
SecureCRT下载地址:http://www.portablesoft.org/securecrt-securefx-legacy-versions/1.打开我们的SecureCRT客户端,点击t ...