spring boot 加入mail邮件支持
一、添加依赖
<!-- 邮件整合 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
二、添加mail.properties配置文件
#设置邮箱主机
spring.mail.host=smtp.qq.com
#设置用户名
spring.mail.username=xxxxxxx
#设置密码
#QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码
spring.mail.password=xxxxxxxxxxxxxxxx
#端口
spring.mail.port=465
#协议
#spring.mail.protocol=smtp
#设置是否需要认证,如果为true,那么用户名和密码就必须的,
#如果设置false,可以不设置用户名和密码,当然也得看你的对接的平台是否支持无密码进行访问的。
spring.mail.properties.mail.smtp.auth=true
#STARTTLS[1] 是对纯文本通信协议的扩展。它提供一种方式将纯文本连接升级为加密连接(TLS或SSL),而不是另外使用一个端口作加密通信。
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
三、加载属性文件
在启动类加上
@PropertySource({ "classpath:mail.properties" })
四、添加MailConfig.java
package com.spring.config; import java.io.File;
import java.util.List;
import java.util.Map; import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper; @Configuration
public class MailConfig { @Resource
private JavaMailSenderImpl mailSender; @Value("${spring.mail.username}")
private String username; /**
* 发送纯文本形式的email
*
* @param toEmail 收件人邮箱
* @param title 邮件标题
* @param content 邮件内容
*/
public void sendTextMail(String toEmail, String title, String content) {
SimpleMailMessage msg = new SimpleMailMessage();
msg.setFrom(username);
msg.setTo(toEmail);
msg.setSubject(title);
msg.setText(content);
mailSender.send(msg);
} /**
* 发送带有html的内容
*
* @param toEmail 收件人邮箱
* @param title 邮件标题
* @param htmlContent 邮件内容
*/
public void sendHtmlMail(String toEmail, String title, String htmlContent) throws MessagingException {
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, false, "utf-8");
helper.setFrom(username);
helper.setTo(toEmail);
helper.setSubject(title);
helper.setText(htmlContent, true);
mailSender.send(msg);
} /**
* 添加附件的email发送
*
* @param toEmail 收件人地址
* @param title 邮件标题
* @param content 文本内容
* @param aboutFiles 附件信息 每个子项都是一个文件相关信息的map Map<String,String>: 1.filePath
* 2.fileName
* @throws Exception 异常
*/
public void sendAttachmentMail(String toEmail, String title, String content, List<Map<String, String>> aboutFiles) throws Exception {
MimeMessage msg = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(msg, true, "utf-8");
helper.setFrom(username);
helper.setTo(toEmail);
helper.setSubject(title);
helper.setText(content);
FileSystemResource resource = null;
for (Map<String, String> file : aboutFiles) {
resource = new FileSystemResource(file.get("filePath"));
if (resource.exists()) {// 是否存在资源
File attachmentFile = resource.getFile();
helper.addAttachment(file.get("fileName"), attachmentFile);
}
}
mailSender.send(msg);
} }
五、使用MailConfig
@Autowired
private MailConfig mailConfig;
使用MailConfig里面的方法发送即可
spring boot 加入mail邮件支持的更多相关文章
- spring boot集成spring-boot-starter-mail邮件功能
前情提要 以目前IT系统功能来看,邮件功能是非常重要的一个功能.例如:找回密码.邮箱验证,邮件动态码.忘记密码,邮件营销等,都需要用到邮件功能.结合当下最流行的spring boot微服务,推出了sp ...
- Spring Boot中的缓存支持(一)注解配置与EhCache使用
Spring Boot中的缓存支持(一)注解配置与EhCache使用 随着时间的积累,应用的使用用户不断增加,数据规模也越来越大,往往数据库查询操作会成为影响用户使用体验的瓶颈,此时使用缓存往往是解决 ...
- Spring Boot项目如何同时支持HTTP和HTTPS协议
如今,企业级应用程序的常见场景是同时支持HTTP和HTTPS两种协议,这篇文章考虑如何让Spring Boot应用程序同时支持HTTP和HTTPS两种协议. 准备 为了使用HTTPS连接器,需要生成一 ...
- Spring Boot (十):邮件服务
Spring Boot 仍然在狂速发展,才几个多月没有关注,现在看官网已经到 2.1.0.RELEASE 版本了.准备慢慢在写写 Spring Boot 相关的文章,本篇文章使用 Spring Boo ...
- (转)Spring Boot (十):邮件服务
http://www.ityouknow.com/springboot/2017/05/06/spring-boot-mail.html Spring Boot 仍然在狂速发展,才几个多月没有关注,现 ...
- Spring Boot实战系列-----------邮件发送
快速导航 添加Maven依赖 配置文件增加邮箱相关配置 Service.Test项目代码构建 五种邮件发送类型讲解 文本邮件 html邮件 附件邮件 html内嵌图片邮件 模板邮件 问题汇总 添加ma ...
- Spring Boot笔记之邮件(spring-boot-starter-mail)
Spring Boot环境中发送邮件 pom.xml引入`spring-boot-starter-mail` application.yml配置 163邮箱 QQ邮箱 Gmail邮箱 发送邮件 ser ...
- Spring Boot Security OAuth2 实现支持JWT令牌的授权服务器
概要 之前的两篇文章,讲述了Spring Security 结合 OAuth2 .JWT 的使用,这一节要求对 OAuth2.JWT 有了解,若不清楚,先移步到下面两篇提前了解下. Spring Bo ...
- Spring boot之添加JSP支持
大纲 (1) 创建Maven web project: (2) 在pom.xml文件添加依赖 (3) 配置application.properties支持jsp (4) 编写测试Controller ...
随机推荐
- 胸部CT提取分割肺部
1. 肺部分割提取简介 在处理胸部CT时,我们常常需要获取肺部的一个mask,也就是将肺部结构从数据中提取出来.二维图像还好说,但是三维图像就会变得复杂复杂一点.肺部的分割常常做后续操作的预处理,所以 ...
- ARTS-S golang函数作为参数传递
函数作为参数传递在单元测试的时候非常有用,看下面的例子. package main import "fmt" func output(f func(string, string, ...
- C++ 并发编程指南(收藏笔记)
git地址: https://github.com/forhappy/Cplusplus-Concurrency-In-Practice https://github.com/forhappy/Cpl ...
- Java多态之动态绑定
目录 Java多态之动态绑定 引用变量的类型 编译时类型 运行时类型 方法绑定 静态绑定 动态绑定 方法表 Java多态之动态绑定 上篇回顾:多态是面向对象程序设计非常重要的特性,它让程序拥有 更好的 ...
- robot_framework常用关键字
快捷键 F8 运行 ctrl+alt+空格 log 类似于print Set variable 定义变量 Catenate 连接对象 SEPARATOR 对多个连接信息进行分割 Create List ...
- LInux内核配置过程
内核版本 linux 2.6.32.2 配置内核的过程 配置内核可以通过执行 make menuconfig 来进行,下面分析该命令的执行流程 执行该目标 %config: scripts_basic ...
- 【Java Web开发学习】Spring构造器和属性注入
测试类 public class Construct { private String address; private long phone; public Construct(String nam ...
- AES-加密解密工具类
注意:AES加密算法对于不同系统之间的tokens可能会不同,需要在相同的系统间使用 如果设置为private,在其它类调用AES访问不了这个tokens常量,需要自主定义一个tokens或者在配置文 ...
- 面试题:为什么客户端最后还要等待2MSL
面试题: 为什么客户端最后还要等待2MSL MSL(Maximum Segment Lifetime),TCP允许不同的实现可以设置不同的MSL值. 保证客户端发送的最后一个ACK报文能够到达服务器, ...
- Rancher 2.3.3发布!默认支持K8S 1.16
2019年11月28日,Rancher Labs发布了Rancher全新版本2.3.3,该版本默认支持Kubernetes1.16,此外还带来了其他功能与优化. 目前,Rancher的Latest和S ...