发送邮件由于是一个耗时的操作,有可能需要一个几十秒的操作,但是呢,接口 是一个瞬间完成的,为了不影响接口的性能,所以需要对发送邮件的操作进行异步操作,我们这里呢,首先我们要引入发送邮件的测试模块。

  

  <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 异步发送邮件的更多相关文章

  1. Spring Boot异步发送邮件和请求拦截器配置

    用户登录流程图: 在spring拦截器中进行鉴权操作: 控制器的拦截: import com.mooc.house.common.model.User; import org.springframew ...

  2. Spring Boot 2发送邮件手把手图文教程

    原文:http://www.itmuch.com/spring-boot/send-email/ 本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有 ...

  3. Spring Boot 之发送邮件

    Spring Boot 之发送邮件 简介 API 配置 实战 引入依赖 配置邮件属性 Java 代码 完整示例 引申和引用 简介 Spring Boot 收发邮件最简便方式是通过 spring-boo ...

  4. Spring Boot (17) 发送邮件

    添加依赖 <!--发送邮件 --> <dependency> <groupId>org.springframework.boot</groupId> & ...

  5. Spring Boot 异步请求和异步调用,一文搞定

    一.Spring Boot中异步请求的使用 1.异步请求与同步请求 特点: 可以先释放容器分配给请求的线程与相关资源,减轻系统负担,释放了容器所分配线程的请求,其响应将被延后,可以在耗时处理完成(例如 ...

  6. SpringBoot系列:Spring Boot异步调用@Async

    在实际开发中,有时候为了及时处理请求和进行响应,我们可能会多任务同时执行,或者先处理主任务,也就是异步调用,异步调用的实现有很多,例如多线程.定时任务.消息队列等, 这一章节,我们就来讲讲@Async ...

  7. Spring boot 中发送邮件

    参考:https://blog.csdn.net/qq_39241443/article/details/81293939 添加依赖: <dependency> <groupId&g ...

  8. Spring Boot 异步运用

    使用@Async标签 导入包 org.springframework.scheduling.annotation.Async 并配置并发线程池asyncTaskConfig 实现AsyncConfig ...

  9. Spring Boot 异步调用

    添加一个类ThreadPoolConfig.java package com.cjcx.inter.framework.config; import org.springframework.conte ...

随机推荐

  1. R中的Regex

    Description grep.grepl.regexpr.gregexpr和regexec在字符向量的每个元素中搜索与参数模式匹配的参数:它们在结果的格式和详细程度上有所不同. sub和gsub分 ...

  2. cooke和session

    一.装饰器要加入funtools.wrap装饰 保留函数的元数据(函数名/注释) 1.装饰器 def wrapper(f): def inner(*args,**kwargs): return f(* ...

  3. SQL中rownumber的用法

    1)一次排名: 语法:row_number() over(order by 字段 desc/asc):按照某个字段排名 1.1.查询语句: 1.2.查询结果:查询结果按照薪水进行排名 2)先分组后排名 ...

  4. 面试官:HashMap死循环形成的原因是什么?

    介绍 HashMap实现原理 之前的文章已经分析了HashMap在JDK1.7的实现,这篇文章就只分析HashMap死循环形成的原因 死循环形成是在扩容转移元素的时候发生的 void resize(i ...

  5. php 生成游戏兑换码(礼包)方法

    最近项目中要做礼包码生成,看了看网上的代码,可以使用php扩展unid 当然我这里并不是用的unid,而是使用的php自带的uniqid,人狠话不多.看代码 /** * 生成礼包接口 100W数据同时 ...

  6. onOK Modal.warning iview 要写一个函数 套上,不然会得不到异步调用,直接弹出的时候就执行了

    export const warning = (str, callback = _ => {}, outCallback = () => {}) => { Modal.warning ...

  7. js 验证输入框是否为空

    很多时候,菜鸟在对输入框是否输入值进行验证时,总会把输入“空字符串”这一状况给忽略掉.嗯,我就这样,所以记下,以后回忆下还是不错滴 <input type="text" id ...

  8. java-方法。(新手)

    import java.util.ArrayList; //导入包.import java.util.Iterator;import java.util.LinkedList;import java. ...

  9. redis 持久化RDB、AOF

    1.redis持久化简介 Redis是一种高级key-value数据库.它跟memcached类似,不过数据可以持久化,而且支持的数据类型很丰富.有字符串,链表,集合和有序集合.支持在服务器端计算集合 ...

  10. 小白学 Python 数据分析(19):Matplotlib(四)常用图表(下)

    人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析(1):数据分析基础 小白学 Python 数据分析(2):Pandas (一)概述 小白学 Python 数据分析(3):P ...