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集成邮件发送功能和接收功能:若对邮件一些基本协议和发送流程不懂的请务必参考我之前写的博客或者浏览 ...
随机推荐
- ubuntu下面安装nodejs
对于刚接触ubuntu的同学来说,一切都是新的,一切都是那么熟悉而又不熟悉的.不管是作为一个前端工程师还是一个后端工程师,我相信大家知道nodejs,但是如果希望自己能够在ubuntu上面使用node ...
- 如何设计APP版本号?
示例: 2.14.21 (主版本号.次版本号.补丁号) 我们可以这样设计,软件包的版本号以英文句号分隔的三个数字来定义,分别代表主版本号.次版本号和补丁号.如果只是修复了错误,没有添加任何功能,也不会 ...
- C#中类的修饰符
Q&A 项目=程序集=assembly 1,Q:类的修饰符有哪些? A: 有 new.public.protect.internal.private.abstract.sealed.st ...
- Python有参函数的使用
1.给定验证码长度n,生成随机验证码,验证码由数字.字母组成(参考chr()内置方法) 程序代码如下: import random def create_check_code(n): check_co ...
- python 对字典分别按照key值、value值进行排序
1.sorted函数首先介绍sorted函数,sorted(iterable,key,reverse),sorted一共有iterable,key,reverse这三个参数. 其中iterable表示 ...
- sed 面试题
#oldboy my qq num is 49000448.$ not 4900000448. my god ,i am not oldbey,but clsn!$ #oldboy my name i ...
- MySQL 优化 (四)
参数优化 innodb_log_buffer_size 此参数确定些日志文件所用的内存大小,以M为单位.缓冲区更大能提高性能,对于较大的事务,可以增大缓存大小. innodb_log_buffer_s ...
- android 圆角ImageView类,可设置弧度
public class RoundImageView extends ImageView { private Paint paint; private int roundWidth = 50; pr ...
- docker 私有registry harbor安装
一,harbor安装: 参考:Installation and Configuration Guide 1,安装docker 2,安装docker compose sudo curl -L " ...
- 机器学习(1)——K近邻算法
KNN的函数写法 import numpy as np from math import sqrt from collections import Counter def KNN_classify(k ...