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 ...
随机推荐
- linux mysql,tomcat与java的安装
先将服务器的安全组设置为 把所有端口或者所需要的端口开放 然后测试远程访问 ssh 用户@ip 登录 输入密码 service iptables stop ...
- 用tensorflow求偏导
# coding:utf-8 from __future__ import absolute_import from __future__ import unicode_literals from _ ...
- Linux-tac、diff、tree、echo、seq、重定向
1.tac 方向输出文件,最后一行放在第一行的位置输出 2.diff 比较文件的内容 vimdiff:在vim中比较 3. tree 树状图显示目录内容 -d 只显示目录 -L 树状 目录 ...
- 【Nodejs】326- 从零开发一个node命令行工具
本文由 IMWeb 社区授权转载自腾讯内部 KM 论坛.点击阅读原文查看 IMWeb 社区更多精彩文章. 什么是命令行工具? 命令行工具(Cmmand Line Interface)简称cli,顾名思 ...
- ruby on rails测试
Rspec测试 Rspec(基本测试) 安装 group :development, :test do gem 'rspec-rails', '~> 3.5' end rails generat ...
- docker入门-镜像管理命令篇
一.下载.上传镜像 1:下载安装centos镜像 语法:docker [参数][镜像名称] [root@host1 ~]# docker pull centos Using default tag ...
- python学习-pandas
import pandas as pd # DataForm 二维数据# print(pd.read_excel("datas.xlsx")) # 多行数据 - 加载表单s = p ...
- 使用Pycharm轻轻松松脱下git版本控制高大上的外衣
一.思考❓❔ 1.git操作难吗? git操作命令繁杂 需求复杂场景, 使用不易 原理深邃,对初学者来说是有难度的 2.那么难,还要学吗? 作为IT行业从业者(搬砖小工),不会Git?滚,出去~~~ ...
- CCF-CSP题解 201809-3 元素选择器
题目要求写一个简易的CSS Selector. 首先用结构体\(<lev,label[],hasId,id[]>\)存储元素.其中\(lev\)表示元素在html树中的深度(这个是因为逻辑 ...
- 牛客NOIP暑期七天营-提高组3
第一题:破碎的矩阵 题目链接:https://ac.nowcoder.com/acm/contest/932/A 刚看到这题的时候感觉特别熟悉...诶,这不就是codeforces某场比赛的某某 ...