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 ...
随机推荐
- 利用python进行微信好友分析
欢迎python爱好者加入:学习交流群 667279387 本文主要利用python对个人微信好友进行分析并把结果输出到一个html文档当中,主要用到的python包为itchat,pandas,py ...
- openstack网络(四)-虚机流量分析
几种网络名词解释 使用LinuxBridge时虚机流量分析 VLAN FLAT Local VXLAN 使用OVS时虚机流量分析 几种网络名词解释 1.local网络:local网络是与其他网络和节点 ...
- HDU-1698-----Just Hook
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- VS2013 MFC 库冲突引起的错误解决
http://www.cnblogs.com/rainbowzc/archive/2010/06/29/1767248.html 7 1>LIBCMT.lib(crt0dat.obj) : er ...
- Python3 面向对象进阶2
目录 Classmethod Staticmethod Isinstance Issubclass 反射 概念 hasattr getattr setattr delattr 魔法方法 概念 __ne ...
- 【JS】370- 总结异步编程的六种方式
点击上方"前端自习课"关注,学习起来~ 作者:Aima https://segmentfault.com/a/1190000019188824 众所周知 JavaScript 是 ...
- 【Nodejs】326- 从零开发一个node命令行工具
本文由 IMWeb 社区授权转载自腾讯内部 KM 论坛.点击阅读原文查看 IMWeb 社区更多精彩文章. 什么是命令行工具? 命令行工具(Cmmand Line Interface)简称cli,顾名思 ...
- win10配置git SSH
1.安装的过程就不说了,直接去官网下载git for windows 安装便可 安装完了,无非就是像用它,就想从github上clone项目下来,仅仅是安装了git还不能直接从远程下载项目下来哦,还需 ...
- .net access config 相对路径
<configuration> <connectionStrings> <add name="ConStr" connectionString=&qu ...
- 一篇文章看清楚JDK13的特性!
1.switch优化更新 JDK11以及之前的版本: switch (day) { case MONDAY: case FRIDAY: case SUNDAY: System.out.println( ...