SpringBoot 2.X从0到1实现邮件发送功能
Spring中提供了JavaMailSender接口实现邮件发送功能,在SpringBoot2.X中也封装了发送邮件相关的Starter并且提供了自动化配置。
本文目录
一、添加对应的Starter二、添加发送邮件相关的配置三、实现发送邮件功能四、实现过程中踩过的坑1.中文附件乱码问题2.无法注入JavaMailSender问题
一、添加对应的Starter
pom.xml中添加下面依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二、添加发送邮件相关的配置
application.properties中添加邮件配置
spring.mail.host = smtp.163.com
spring.mail.port = 465
spring.mail.username = xxx@163.com
spring.mail.password = xxx
spring.mail.properties.mail.smtp.auth = true
spring.mail.properties.mail.smtp.timeout = 25000
spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.starttls.enable = true
spring.mail.properties.mail.smtp.starttls.required = true
三、实现发送邮件功能
本例中封装了一个MailServiceImpl,里面注入javaMailSender即可,具体代码如下
MailServiceImpl.java部分代码:
/**
* 发送邮件
*
* @Author: java碎碎念
*/
public void sendMail(Email email) {
long start = System.currentTimeMillis();
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
helper.setFrom(mailUserName);
helper.setTo(email.getToAddress().toArray(new String[email.getToAddress().size()]));
helper.setSubject(email.getSubject());
helper.setText(email.getContent(), true);
if (null != email.getAttachments() && email.getAttachments().size() > 0) {
for (File curFile : email.getAttachments()) {
FileSystemResource file = new FileSystemResource(curFile);
helper.addAttachment(MimeUtility.encodeWord(file.getFilename(), "utf-8", "B"), file);
}
}
log.info("邮件开始发送");
javaMailSender.send(mimeMessage);
long sendMillTimes = System.currentTimeMillis() - start;
log.info("邮件发送成功,sendTimes=" + sendMillTimes);
} catch (Exception e) {
log.error("发送html邮件时发生异常!", e);
}
}
MailController.java部分代码:
@RequestMapping(value = "/sendMail")
public String sendEmail() {
Email email_email = new Email();
List<String> addressList = new ArrayList<String>();
addressList.add("xxx@qq.com");
email_email.setToAddress(addressList);
email_email.setSubject("java碎碎念-主题测试");// 主题
email_email.setContent("你好!<br><br> 测试邮件发送成功!");
// 发送邮件
mailService.sendMail(email_email);
return "ok";
}
项目启动成功后访问http://localhost:8080/sendMail即可成功发送邮件,收到邮件截图如下:

收到邮件截图
到此发送邮件功能全部实现,有问题欢迎留言沟通哦!
完整源码地址: https://github.com/suisui2019/springboot-study
四、实现过程中踩过的坑
1.中文附件乱码问题
java mail发邮件是附件名过长默认会被截断显示乱码,在程序启动时设置主动设置为false可正常显示,具体设置代码如下
public static void main(String[] args) {
//java mail发邮件时附件名过长默认会被截断,附件名显示【tcmime.29121.29517.50430.bin】,主动设为false可正常显示附件名
System.setProperty("mail.mime.splitlongparameters", "false");
SpringApplication.run(SendMailApplication.class, args);
}
2.无法注入JavaMailSender问题
在application.properties中配置spring.mail.host即可。
推荐阅读
1.Spring Boot入门-快速搭建web项目
2.Spring Boot 2.X 整合Redis
3.Spring Boot 2.X 如何优雅的解决跨域问题?
4.Spring Boot 2.X 如何添加拦截器?
5.Spring Boot 2.X 集成spring session实现session共享
6.Redis Cluster搭建高可用Redis服务器集群
7.为什么单线程的Redis这么快?
8.一篇文章搞定SpringMVC参数绑定
9.SpringMVC+Mybatis 如何配置多个数据源并切换?
10.Spring条件注解@Conditional
限时领取免费Java相关资料,涵盖了Java、Redis、MongoDB、MySQL、Zookeeper、Spring Cloud、Dubbo/Kafka、Hadoop、Hbase、Flink等高并发分布式、大数据、机器学习等技术。
关注下方公众号即可免费领取:
Java碎碎念公众号
SpringBoot 2.X从0到1实现邮件发送功能的更多相关文章
- Spring Boot 2.0 图文教程 | 集成邮件发送功能
文章首发自个人微信公众号: 小哈学Java 个人网站: https://www.exception.site/springboot/spring-boots-send-mail 大家好,后续会间断地奉 ...
- 用ASP.NET Core 1.0中实现邮件发送功能
准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试 ...
- springboot下实现邮件发送功能
springboot给我们封装好了邮件功能,非常简单,只需要稍微配置下就ok. 引入jar <dependency> <groupId>org.springframework. ...
- 用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇
在上篇中用MailKit实现了Asp.net core 邮件发送功能,但一直未解决阿里云邮件推送问题,提交工单一开始的回复不尽如人意,比如您的网络问题,您的用户名密码不正确等,但继续沟通下阿里云客户还 ...
- SpringBoot邮件发送功能
快速入门 在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖: <dependency> <groupId>org.sp ...
- springboot做邮件发送功能时报错No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender' available:的问题解决方案
1.检查application.yml中的配置是否正确 spring.mail.host=smtp.xxx.comspring.mail.username=xxx@xxx.comspring.mail ...
- springboot添加邮件发送及压缩功能
springboot添加邮件发送及文件压缩功能 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190233.html 先来一段诗 ``` 就这样吧 忍受折磨 ...
- SpringBoot | 第二十六章:邮件发送
前言 讲解了日志相关的知识点后.今天来点相对简单的,一般上,我们在开发一些注册功能.发送验证码或者订单服务时,都会通过短信或者邮件的方式通知消费者,注册或者订单的相关信息.而且基本上邮件的内容都是模版 ...
- SpringBoot集成邮件发送
一:简述 在日常中的工作中难免会遇到程序集成邮件发送功能.接收功能:此篇文章我将使用SpringBoot集成邮件发送功能和接收功能:若对邮件一些基本协议和发送流程不懂的请务必参考我之前写的博客或者浏览 ...
随机推荐
- 【oracle】ORA-06550 字符串长度限制在范围
number(2)输入了100 就会导致异常
- 多个线程运行MR程序时hadoop出现的问题
夜间多个任务同时并行,总有几个随机性有任务失败,查看日志: cat -n ads_channel.log |grep "Caused by" Caused by: java.uti ...
- 【RTOS】基于V7开发板的最新版RTX4 V4.81.1程序模板,不使用CMSIS-RTOS封装层,继续保持超强战斗力
模板下载: 链接:https://pan.baidu.com/s/1idoQYcR3SOzVC3KTFcMGMA 提取码:i8k5 1.MDK使用MDK5.26及其以上版本. 2.进入到MDK5后 ...
- (六十三)c#Winform自定义控件-箭头(工业)-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- GO语言介绍以及开发环境配置
一.介绍 GO语言是静态强类型语言 静态也就是编译型语言 二.安装 1.下载地址 下载地址 https://golang.google.cn/dl/ 2.安装 Linux安装 1.下载二进制包:go1 ...
- 蓝色大气简约立体答辩ppt模板推荐
小编个人非常喜欢这个模版,大气深蓝色,具有科技感,非常适合学生的毕业答辩PPT模板. 模版来源:http://ppt.dede58.com/gongzuohuibao/26496.html
- Bootstrap基本CSS样式
一.简介.使用 1.简介 Bootstrap 来源于 Twitter,是一款基于 Html.Css.JavaScript 的前端UI框架.可以方便.快速的开发web界面. 教程:https://www ...
- 利用Azure虚拟机安装Dynamics 365 Customer Engagement之一:准备工作
我是微软Dynamics 365 & Power Platform方面的工程师罗勇,也是2015年7月到2018年6月连续三年Dynamics CRM/Business Solutions方面 ...
- linux学习(四)复制(cp)移动(mv)删除(rm)查找(find)文件、文件夹操作、软硬链接的区别
目录 复制文件 mv命令 rm命令 touch 命令 file命令 find命令 grep命令 mkdir命令 rmdir命令 @(复制移动删除查找文件.软硬链接的区别) 复制文件 cp命令用于复制文 ...
- docker 存储卷 Volumes
一,docker容器面临的困境: 容器运行中产生的数据,是放到容器栈的最顶层,当容器停止并被删除后,这些数据就被删除了. docker采用COW(写时复制)策略,导致性能低下.比如有个mysql容器, ...