SpringBoot2.x整合JavaMail以qq邮箱发送邮件
本文参考spring官网email接口文档所写。
spring-email官方网址:https://docs.spring.io/spring/docs/5.1.8.RELEASE/spring-framework-reference/integration.html#mail
1:相关依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
2:邮箱收件人model
package com.dev.model.email; import java.io.Serializable; /**
* Created on 2019-08-02 16:16.
*
* @author zgq7
*/
public class EmailModel implements Serializable { /**
* 收件人姓名
**/
private String recieverName; /**
* 收件人邮箱地址
**/
private String recieverEmailAddress; /**
* 邮件主题
**/
private String emailTheme; /**
* 邮件内容
**/
private String emailContent; public String getRecieverName() {
return recieverName;
} public void setRecieverName(String recieverName) {
this.recieverName = recieverName;
} public String getRecieverEmailAddress() {
return recieverEmailAddress;
} public void setRecieverEmailAddress(String recieverEmailAddress) {
this.recieverEmailAddress = recieverEmailAddress;
} public String getEmailTheme() {
return emailTheme;
} public void setEmailTheme(String emailTheme) {
this.emailTheme = emailTheme;
} public String getEmailContent() {
return emailContent;
} public void setEmailContent(String emailContent) {
this.emailContent = emailContent;
}
}
3:邮箱工具类
package com.dev.utils.email; import com.dev.model.email.EmailModel;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator; import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; /**
* Created on 2019-08-02 16:06.
*
* @author zgq7
*/
public class MailSendUtils { private static final Logger logger = LoggerFactory.getLogger(MailSendUtils.class); /**
* 发送者地址
**/
private static String posterAdress = "xxx@qq.com"; /**
* 发送者姓名
**/
private static final String posterName = "xxx"; @Autowired
private JavaMailSender javaMailSender; @Autowired
private JavaMailSenderImpl javaMailSenderImpl; /**
* 文本发送
**/
public void sendEmailAsText(final EmailModel emailModel) {
MimeMessagePreparator mimeMessagePreparator = mimeMessage -> {
mimeMessage.setFrom(posterAdress);
mimeMessage.setRecipients(Message.RecipientType.TO, emailModel.getRecieverEmailAddress());
mimeMessage.setText("<html><body>"
+ "hello:" + emailModel.getRecieverName()
+ "<br>" + "msg:" + emailModel.getEmailContent()
+ "<br>" + "from :" + posterName
+ "</body></html>");
};
try {
this.javaMailSender.send(mimeMessagePreparator);
logger.info("邮箱已返送至[{}]邮箱!", emailModel.getRecieverName());
} catch (MailException e) {
logger.error("邮箱异常:{}", e);
}
} /**
* html 网页发送
* 该方法为同步方法
**/
public void sendEmailAsSysExceptionHtml(final EmailModel emailModel) {
MimeMessage mimeMessage = javaMailSenderImpl.createMimeMessage();
MimeMessageHelper mimeMessageHelper;
try {
mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
mimeMessageHelper.setFrom(posterAdress);
mimeMessageHelper.setTo(emailModel.getRecieverEmailAddress());
mimeMessageHelper.setText("<!DOCTYPE html>\n" +
"<html>\n" +
"\t<head>\n" +
"\t\t<meta charset=\"UTF-8\">\n" +
"\t\t<title></title>\n" +
"\t</head>\n" +
"\t<style>\n" +
"\t\tbody,\n" +
"\t\ttable,\n" +
"\t\ttbody,\n" +
"\t\ttr {\n" +
"\t\t\tbackground-color: aquamarine;\n" +
"\t\t\tbackground-size: 100%;\n" +
"\t\t}\n" +
"\t</style>\n" +
"\n" +
"\t<body>\n" +
"\t\t<table border=\"solid 2 px\" align=\"center\" style=\"text-align: center;\">\n" +
"\t\t\t<tbody>\n" +
"\t\t\t\t<tr>\n" +
"\t\t\t\t\t<td width=\"200px\" bgcolor=\"coral\">时间</td>\n" +
"\t\t\t\t\t<td width=\"80%\" bgcolor=\"azure\">" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + "</td>\n" +
"\t\t\t\t</tr>\n" +
"\t\t\t\t<tr>\n" +
"\t\t\t\t\t<td width=\"200px\" bgcolor=\"coral\">信息</td>\n" +
"\t\t\t\t\t<td width=\"80%\" bgcolor=\"azure\">" + "测试" + "</td>\n" +
"\t\t\t\t</tr>\n" +
"\t\t\t\t<tr>\n" +
"\t\t\t\t\t<td width=\"200px\" bgcolor=\"coral\">堆栈</td>\n" +
"\t\t\t\t\t<td width=\"80%\" bgcolor=\"azure\" style=\"text-align: left;\">" + emailModel.getEmailContent() + "</td>\n" +
"\t\t\t\t</tr>\n" +
"\t\t\t</tbody>\n" +
"\t\t</table>\n" +
"\t</body>\n" +
"\n" +
"</html>"
, true); this.javaMailSender.send(mimeMessage);
logger.info("邮箱已返送至[{}]邮箱!", emailModel.getRecieverName()); } catch (MessagingException e) {
e.printStackTrace();
} catch (MailException e) {
logger.error("邮箱异常:{}", e);
}
} }
notice3.1:可自行设置文本发送api、html发送api、图片发送api等。我的工具包中只写了文本发送和html发送两种格式的api。
4:注册EmailUtils的bean
package com.dev.config;
import com.dev.utils.email.MailSendUtils;
/**
* Created by zgq7 on 2019/6/6.
* 注册一些bean进入ioc
*
*/
@Configuration
public class BeanRegistryCenterConfig {
/**
* 邮箱工具类 bean 注册
**/
@Bean
public MailSendUtils mailSendUtils() {
return new MailSendUtils();
} }
5:Application.properties配置
spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=你的qq@qq.com
spring.mail.password=xxxxx
spring.mail.protocol=smtp
spring.mail.default-encoding=UTF-8
notice1:spring.mail.usernam,是你的qq邮箱地址
notice2:spring.mail.password,不要用真正的qq邮箱密码,而是qq邮箱第三方服务授权码
下面是获取qq邮箱第三方服务授权码的操步骤:
5.1:登录qq邮箱,进入首页,点击设置按钮

5.2:点击账户设置

并拉到最下面点击我圈中的开启按钮

然后会弹出一个小框提示你发信息,发就行了,发送之后点击验证会收到授权码


除了spring.mail.username、spring.mail.password 两项要换成自己的 ,其他的和我一样就行;
6:邮件发送测试
6.1:文本发送测试
/**
* Java Mail 文本发送
**/
@Test
public void EmaiTest() {
EmailModel emailModel = new EmailModel();
emailModel.setEmailTheme("测试");
emailModel.setRecieverName("测试");
emailModel.setEmailContent("测试");
emailModel.setRecieverEmailAddress("xxx@qq.com"); mailSendUtils.sendEmailAsText(emailModel);
}
发送成功后:

目标邮箱收到的邮件:

6.2:html发送测试
/**
* Java Mail 网页发送
**/
@Test
public void EmailTest2() throws MessagingException, InterruptedException {
EmailModel emailModel = new EmailModel();
emailModel.setEmailTheme("测试");
emailModel.setRecieverName("测试");
emailModel.setEmailContent("测试");
emailModel.setRecieverEmailAddress("xxx@qq.com"); mailSendUtils.sendEmailAsSysExceptionHtml(emailModel); }
目标邮箱收到的邮件:

这个展示效果可以自己设置。
7:注意点
发送邮件者的邮箱地址为qq邮箱,否则会QQ邮箱认证报错。而接收者邮箱可以为其他的邮箱,如企业邮箱、网易邮箱等。

SpringBoot2.x整合JavaMail以qq邮箱发送邮件的更多相关文章
- 基于java mail实现简单的QQ邮箱发送邮件
刚学习到java邮件相关的知识,先写下这篇博客,方便以后翻阅学习. -----------------------------第一步 开启SMTP服务 在 QQ 邮箱里的 设置->账户里开启 S ...
- 杂项之使用qq邮箱发送邮件
杂项之使用qq邮箱发送邮件 本节内容 特殊设置 测试代码 1. 特殊设置 之前QQ邮箱直接可以通过smtp协议发送邮件,不需要进行一些特殊的设置,但是最近使用QQ邮箱测试的时候发现以前使用的办法无法奏 ...
- ecshop QQ邮箱发送邮件服务器配置
ecshop QQ邮箱发送邮件服务器配置 1.邮件服务:采用其他的SMTP服务 2.邮件服务器是否要求加密连接(SSL): 是 此项设置需要php支持openSSL模块 开启方法: a.php.ini ...
- SpringBoot使用qq邮箱发送邮件
最近公司要做一个邮箱注册和重置密码的功能,因为之前就做过,但是不是Springboot项目,所以相对来说还是比较容易的,在这里记录一下. 一.引用Maven依赖 这里使用spring自带的邮件jar包 ...
- python qq邮箱发送邮件
使用qq发送邮件 # coding=utf8 """ qq邮箱发送邮件 """ import sys reload(sys) sys.set ...
- python3通过qq邮箱发送邮件
python3通过qq邮箱发送邮件 0.了解qq邮箱的SMTP QQ邮箱 POP3 和 SMTP 服务器地址设置如下: 邮箱 POP3服务器(端口995) SMTP服务器(端口465或587) qq. ...
- 使用 QQ 邮箱发送邮件报错:java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.MessagingException: Exception reading response
使用 QQ 邮箱发送邮件报错:java.net.SocketTimeoutException: Read timed out. Failed messages: javax.mail.Messagin ...
- legend3---lavarel中使用qq邮箱发送邮件
legend3---lavarel中使用qq邮箱发送邮件 一.总结 一句话总结: 第一步:配置邮箱做服务器,比如qq邮箱,网易163邮箱 第二步:配置lavarel的配置文件 第三部:写邮件发送代码就 ...
- 用JavaMail通过QQ邮箱来发送邮件(第一篇博客,备忘)
1.先启用QQ邮箱里POP3/STMP服务:生成授权码 2.导入mail.jar包(不要用太古董的技术,你懂得) 3.注意要在代码里加上开启SSL加密的代码 4.直接上代码 import java.u ...
随机推荐
- 分布式-springboot基础入门
B站播放地址:https://www.bilibili.com/video/BV1PE411i7CV?t=51 博客地址:https://www.cnblogs.com/hellokuangshen/ ...
- jvm系列二内存结构
二.内存结构 整体架构 1.程序计数器 作用 用于保存JVM中下一条所要执行的指令的地址 特点 线程私有 CPU会为每个线程分配时间片,当当前线程的时间片使用完以后,CPU就会去执行另一个线程中的代码 ...
- [译]Rxjs&Angular-退订可观察对象的n中方式
原文/出处: RxJS & Angular - Unsubscribe Like a Pro 在angular项目中我们不可避免的要使用RxJS可观察对象(Observables)来进行订阅( ...
- FZU - 1901 Period II (kmp)
传送门:FZU - 1901 题意:给你个字符串,让你求有多少个p可以使S[i]==S[i+P] (0<=i<len-p-1). 题解:这个题是真的坑,一开始怎么都觉得自己不可能错,然后看 ...
- Codeforces Round #649 (Div. 2) B. Most socially-distanced subsequence
题目链接:https://codeforces.com/contest/1364/problem/B 题意 给出大小为 $n$ 的一个排列 $p$,找出子序列 $s$,使得 $|s_1-s_2|+|s ...
- 【noi 2.6_1759】LIS 最长上升子序列(DP,3种解法)
题意我就不写了.解法有3种: 1.O(n^2).2重循环枚举 i 和 j,f[i]表示前 i 位必选 a[i] 的最长上升子序列长度,枚举a[j]为当前 LIS 中的前一个数. 1 #include& ...
- 【noi 2.6_8786】方格取数(DP)
题意:N*N的方格图每格有一个数值,要求从左上角每步往右或往下走到右下角,问走2次的最大和. 解法:走一次的很好想,而走2次,不可误以为先找到最大和的路,再找剩下的最大和的路就是正解.而应该认清动态规 ...
- Codeforces Round #295 (Div. 2) B. Two Buttons (DP)
题意:有两个正整数\(n\)和\(m\),每次操作可以使\(n*=2\)或者\(n-=1\),问最少操作多少次使得\(n=m\). 题解:首先,若\(n\ge m\),直接输出\(n-m\),若\(2 ...
- Atcoder Beginner Contest 168 D - .. (Double Dots) (BFS)
题意:有\(n\)个房间,在这些房间中两两连\(m\)次条边,问除了第一个房间,其他房间走到第一个房间的最短路径,输出这个房间所连的上一个房间,如果走不到,输出\(no\). 题解:刚开始我写了一个d ...
- 前端模块化之ES Module
一.概述 之前提到的几种模块化规范:CommonJS.AMD.CMD都是社区提出的.ES 2015在语言层面上实现了模块功能,且实现简单,可以替代CommonJS和AMD规范,成为在服务器和浏览器通用 ...