spring boot 异步发送邮件
发送邮件由于是一个耗时的操作,有可能需要一个几十秒的操作,但是呢,接口 是一个瞬间完成的,为了不影响接口的性能,所以需要对发送邮件的操作进行异步操作,我们这里呢,首先我们要引入发送邮件的测试模块。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
引入包后呢,我们去配置需要的邮件相关的配置,
mail:
host: smtp.qq.com
port: 587
username: 952943386@qq.com
password: 需要在你用的邮箱那里配置
default-encoding: utf-8
properties:
mail:
smtp:
socketFactoryClass: javax.net.ssl.SSLSocketFactory
debug: true
这样配置完毕之后呢,就可以发送邮件了,我们利用异步,首先我们先编写一个发送邮件的接口
public interface EmailServer {
void sendemail(String subject,String from ,String touserEmail,String text);
}
我们去实现这个接口,
@Component
@Service
public class EmailServerImpl implements EmailServer {
@Autowired
private JavaMailSender javaMailSender;
@Async("taskExecutor")
@Override
public void sendemail(String subject, String from, String touserEmail, String text) {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(subject);
message.setFrom(from);
message.setTo(touserEmail);
message.setSentDate(new Date());
message.setText(text);
javaMailSender.send(message);
}
}
实现完毕后呢,我们这里已经配置完毕了,我们就可以在正常 的业务去调用了。
我这里改的找回密码的逻辑。
if (user.getEmail() != null) {
emailServerl.sendemail("全栈测试平台修改密码",user.getEmail(),
user.getEmail(),"你的密码修改成功,用户名:" + user.getUsername());
}
这里,需要在上面去引入
@Autowired
private EmailServer emailServerl;
这样我们就已经开发完毕了,我们还需要配置启动的时候,启动异步。
SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class}, scanBasePackages = "pan")
@EnableScheduling
@EnableAsync//增加这里即可,
public class PlanApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(PlanApplication.class, args);
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(PlanApplication.class);
}
@Bean
MeterRegistryCustomizer meterRegistryCustomizer(MeterRegistry meterRegistry) {
return meterRegistry1 -> {
meterRegistry.config()
.commonTags("application", "Tenantapp");
};
}
}
配置完毕后,我们需要配置下异步任务的配置
@Configuration
public class TaskPoolConfig {
@Bean("taskExecutor")
public Executor taskExecutor () {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数10:线程池创建时候初始化的线程数
executor.setCorePoolSize(10);
// 最大线程数20:
executor.setMaxPoolSize(15);
// 缓冲队列200:
executor.setQueueCapacity(200);
// 允许线程的空闲时间60秒:
executor.setKeepAliveSeconds(60);
// 线程池名的前缀:
executor.setThreadNamePrefix("taskExecutor-");
/*
线程池对拒绝任务的处理策略:这里采用了CallerRunsPolicy策略,
*/
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
// 设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
executor.setWaitForTasksToCompleteOnShutdown(true);
// 设置线程池中任务的等待时间,如果超过这个时候还没有销毁就强制销毁,
executor.setAwaitTerminationSeconds(600);
return executor;
}
}
这样,我们就完成了整体的代码开发,我们去调用下我们的api测试下

测试完毕,接口返回正常,我们去看下,我们的日志有没有执行我们的发送邮件。
Hibernate: select user0_.id as id1_70_, user0_.admin as admin2_70_, user0_.email as email3_70_, user0_.errornum as errornum4_70_, user0_.freeze as freeze5_70_, user0_.freezetime as freezeti6_70_, user0_.iphone as iphone7_70_, user0_.password as password8_70_, user0_.status as status9_70_, user0_.token as token10_70_, user0_.username as usernam11_70_ from user user0_ where user0_.username=?
。。。。。。。。 250 OK: queued as.
DEBUG SMTP: message successfully delivered to mail server
QUIT
221 Bye.
日志打印,我们看下正常我们应该可以看到邮件的,打开qq邮箱,收到了这封邮件。

这样我们的异步发送邮件就修改成功了,完成了我们异步的发送邮件开发。后续封装下发送其他类型的模块,就可以完成了,我们的异步发送邮件的
spring boot 异步发送邮件的更多相关文章
- Spring Boot异步发送邮件和请求拦截器配置
用户登录流程图: 在spring拦截器中进行鉴权操作: 控制器的拦截: import com.mooc.house.common.model.User; import org.springframew ...
- Spring Boot 2发送邮件手把手图文教程
原文:http://www.itmuch.com/spring-boot/send-email/ 本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有 ...
- Spring Boot 之发送邮件
Spring Boot 之发送邮件 简介 API 配置 实战 引入依赖 配置邮件属性 Java 代码 完整示例 引申和引用 简介 Spring Boot 收发邮件最简便方式是通过 spring-boo ...
- Spring Boot (17) 发送邮件
添加依赖 <!--发送邮件 --> <dependency> <groupId>org.springframework.boot</groupId> & ...
- Spring Boot 异步请求和异步调用,一文搞定
一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...
- SpringBoot系列:Spring Boot异步调用@Async
在实际开发中,有时候为了及时处理请求和进行响应,我们可能会多任务同时执行,或者先处理主任务,也就是异步调用,异步调用的实现有很多,例如多线程.定时任务.消息队列等, 这一章节,我们就来讲讲@Async ...
- Spring boot 中发送邮件
参考:https://blog.csdn.net/qq_39241443/article/details/81293939 添加依赖: <dependency> <groupId&g ...
- Spring Boot 异步运用
使用@Async标签 导入包 org.springframework.scheduling.annotation.Async 并配置并发线程池asyncTaskConfig 实现AsyncConfig ...
- Spring Boot 异步调用
添加一个类ThreadPoolConfig.java package com.cjcx.inter.framework.config; import org.springframework.conte ...
随机推荐
- Simulink仿真入门到精通(二) Simulink模块
2.1 Simulink模块的组成要素 用户构建系统模型时无需直接面对成千上万行的代码,而是通过模块化图形界面以模块化的方式构建,能够使理解变得容易,让大脑减负.通过层次化模块分布将系统功能模块化,而 ...
- 性能测试进阶:(一)性能测试工具Locust
An open source load testing tool. 一个开源性能测试工具. define user behaviour with python code, and swarm your ...
- JavaScript每日学习日记(2)
8.13.2019 1. 正则表达式常见字符串方法: search( ) , replace( ) var str = "Visit Website"; var n = str.s ...
- wpf xaml CS0426 错误原因
wpf 程序集中 类命名空间名称和类名不能相同,否则在 xaml生成 i.g.cs时,会导致 自动生成代码无法推到处是类型还是命名空间的问题. 触发这个错误的条件是类命名空间 与 类名相同 并 ...
- JS中iframe子页面与父页面之间通信
iframe子页面与父页面通信根据iframe中src属性是同域链接还是跨域链接,通信方式也不同. 一.同域下父子页面的通信 父页面parent.html <html> <head& ...
- Ubuntu16.04安装QQ机器人
Ubuntu安装QQ机器人 看了看现在QQ机器人似乎只有酷Q机器人有Docker可以在linux上运行了 那就k开始装酷Q机器人,资源占用也不是很大,大概占用180M内存吧 安装酷Q HTTP 首先安 ...
- P5663 加工零件 题解
原题链接 简要题意: 给定一个图,每次询问从 \(x\) 节点开始,\(y\) 步能不能达到 \(1\) 号节点. 算法一 这也是我本人考场算法.就是 深搜 . 因为你会发现,如果 \(x\) 用 \ ...
- Contest 157
2019-10-06 12:15:28 总体感受:总体难度一般,dfs和dp题花了点时间,最后一题dp有思路,但是实现上不够好. 注意点:首先是hard问题的覆盖度依然是很大的问题,其次是要注意审题. ...
- 【Unity游戏开发】跟着马三一起魔改LitJson
一.引子 在游戏开发中,我们少不了和数据打交道,数据的存储格式可谓是百花齐放,xml.json.csv.bin等等应有尽有.在这其中Json以其小巧轻便.可读性强.兼容性好等优点受到广大程序员的喜爱. ...
- windows10删除用户头像
点击开始菜单,然后这里我们点击最上方的用户,弹出的界面,点击这里的更改帐户设置,大家如图进行操作,点击这里即可. 这里我们通过浏览可以修改自己的账户头像,问题是怎么删除这里使用过的账户头像呢?这里 ...