spring boot集成spring-boot-starter-mail邮件功能
前情提要
以目前IT系统功能来看,邮件功能是非常重要的一个功能。例如:找回密码、邮箱验证,邮件动态码、忘记密码,邮件营销等,都需要用到邮件功能。结合当下最流行的spring boot微服务,推出了spring-boot-starter-mail邮件支持包。
功能使用
引入maven包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
相关参数配置(以QQ邮箱为例)
private static JavaMailSenderImpl javaMailSender;
static {
javaMailSender = new JavaMailSenderImpl();
javaMailSender.setHost("smtp.qq.com");//链接服务器
//javaMailSender.setPort(25);//默认使用25端口发送
javaMailSender.setUsername("QQ邮箱");//账号
javaMailSender.setPassword("授权码");//授权码
javaMailSender.setDefaultEncoding("UTF-8");
Properties properties = new Properties();
//properties.setProperty("mail.debug", "true");//启用调试
//properties.setProperty("mail.smtp.timeout", "1000");//设置链接超时
//设置通过ssl协议使用465端口发送、使用默认端口(25)时下面三行不需要
properties.setProperty("mail.smtp.auth", "true");//开启认证
properties.setProperty("mail.smtp.socketFactory.port", "465");//设置ssl端口
properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
javaMailSender.setJavaMailProperties(properties);
}
邮件发送代码(此方法支持文本和html邮件)
@ResponseBody
@RequestMapping("/mailSend")
public void mailSend(){
log.info("--------------[mail/mailSend] start------------------");
try {
MimeMessage message=javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(message,true);
helper.setFrom("272286717@qq.com","272286717");
helper.setTo("qsf179636252@163.com");
helper.setSubject("测试邮件");
helper.setText("测试邮件内容",true);
javaMailSender.send(message);
} catch (Exception e) {
log.error("邮件发送失败", e.getMessage());
e.printStackTrace();
}
log.info("--------------[mail/mailSend] end------------------");
}
测试
测试地址
http://localhost:6677/api/mail/mailSend
日志输出

登录163邮箱查看是否收到邮件

总结
- spring boot对mail的封装支持非常好,使用方便,简单几行代码就可以把邮件集成进来
- QQ邮箱和163邮箱,使用的授权码,而不是登录密码,如果是邮箱登录密码,邮件是发不出去的
spring boot集成spring-boot-starter-mail邮件功能的更多相关文章
- Spring Boot集成Spring Data Reids和Spring Session实现Session共享
首先,需要先集成Redis的支持,参考:http://www.cnblogs.com/EasonJim/p/7805665.html Spring Boot集成Spring Data Redis+Sp ...
- SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- Spring Boot 集成spring security4
项目GitHub地址 : https://github.com/FrameReserve/TrainingBoot Spring Boot (三)集成spring security,标记地址: htt ...
- Spring MVC集成Spring Data Reids和Spring Session实现Session共享
说明:Spring MVC中集成Spring Data Redis和Spring Session时版本是一个坑点,比如最新版本的Spring Data Redis已经不包含Jedis了,需要自行引入. ...
- spring boot2X集成spring cloud config
Spring Cloud Config 分为 Config Server: 分布式配置中心,是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息 Config Client: 通过指定 ...
- Spring Boot 集成 Spring Security 实现权限认证模块
作者:王帅@CodeSheep 写在前面 关于 Spring Security Web系统的认证和权限模块也算是一个系统的基础设施了,几乎任何的互联网服务都会涉及到这方面的要求.在Java EE领 ...
- Spring boot集成spring session实现session共享
最近使用spring boot开发一个系统,nginx做负载均衡分发请求到多个tomcat,此时访问页面会把请求分发到不同的服务器,session是存在服务器端,如果首次访问被分发到A服务器,那么se ...
- Spring boot 集成Spring Security
依赖jar <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- SpringBoot系列:Spring Boot集成Spring Cache
一.关于Spring Cache 缓存在现在的应用中越来越重要, Spring从3.1开始定义了org.springframework.cache.Cache和org.springframework. ...
随机推荐
- wamp环境搭建(Apache2.4.34+PHP7.2.7+MySQL5.5.60)
1 添加环境变量 1.1 添加Apache bin目录 1.2 添加PHP目录 2 配置Apache 2.1 修改conf/httpd.conf 将第38行SRVROOT值修改为当前Apache文件夹 ...
- hdu 5745 La Vie en rose(2016多校第二场)
La Vie en rose Time Limit: 14000/7000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- uva 100 The 3n + 1 problem (RMQ)
uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= ...
- mysql怎么创建,删除,查看索引?
mysql是一个开源的应用非常广泛的数据库.mysql里面的索引能利用利用指针,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万倍.那么,怎么创建索引 ...
- Eclipse设置默认编码为UTF-8
需要设置的几处地方为: Window->Preferences->General ->Content Type->Text->JSP 最下面设置为UTF-8 Window ...
- Spring Boot JPA 懒加载
最近在使用spring jpa 的过程中经常遇到懒加载的错误:"` org.hibernate.LazyInitializationException: could not initiali ...
- siblings() 获得匹配集合中每个元素的同胞
定义和用法 siblings() 获得匹配集合中每个元素的同胞,通过选择器进行筛选是可选的. 如果给定一个表示 DOM 元素集合的 jQuery 对象,.siblings() 方法允许我们在 DOM ...
- Vue打包文件放在服务器,浏览器存在缓存问题的解决
在入口文件index.html添加 <meta http-equiv="pragram" content="no-cache"> <meta ...
- HDU 2454"Degree Sequence of Graph G"(度序列可图性判断)
传送门 参考资料: [1]:图论-度序列可图性判断(Havel-Hakimi定理) •题意 给你 n 个非负整数列,判断这个序列是否为可简单图化的: •知识支持 握手定理:在任何无向图中,所有顶点的度 ...
- ZOJ——Knight Moves(bfs)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...