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

  

  <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. 3L-最好、最坏、平均、均摊时间复杂度

    关注公众号 MageByte,设置星标点「在看」是我们创造好文的动力.后台回复 "加群" 进入技术交流群获更多技术成长. 本文来自 MageByte-青叶编写 上次我们说过 时间复 ...

  2. vue基础----修饰符,watch,computed,method实例方法

    1.vue常用的修饰符,number,trim,number--->当作数字,trim-->去掉前后空格 2.methods与计算属性 computed 的相同与区别 <body&g ...

  3. async,await怎么用

    async声明一个函数是异步的,await用于等待异步完成,并且await只能在async中使用. 使用async,await并行处理请求,速度减半: 将多个promise直接发起请求,先执行asyn ...

  4. C 和 C++语言中的内存拷贝函数memcpy()

    memcpy指的是C和C++使用的内存拷贝函数 函数原型为void *memcpy(void *destin, void *source, unsigned n): 函数的功能是从源内存地址的起始位置 ...

  5. yield 语法备忘录

    yield 语法备忘录     yield 语法备忘录 语法 .net yield 读作:“一有得” 英式发音 皮一下~ yield 关键字向编译器指示它所在的方法是迭代器块. 编译器生成一个类来实现 ...

  6. Journal of Proteome Research | Proteomic analysis of Rhizobium favelukesii LPU83 in response to acid stress.(酸胁迫下根瘤菌LPU83(Rhizobium favelukesii)的蛋白质组学分析)(解读人:丑天胜)

    文献名:Proteomic analysis of Rhizobium favelukesii LPU83 in response to acid stress.(酸胁迫下根瘤菌LPU83(Rhizo ...

  7. jquery 获取url携带的参数

    url= "/page/employee/employeeUpdate.html?id="+data.id 获取 url携带的参数 -> $.getUrlParam = fu ...

  8. Redis 【常识与进阶】

    Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Redis支持数据的持久 ...

  9. django中基于python3.6使用容联发送短信

    一. Django基于python3.6使用容联发送短信流程 容联官方的python支持2.7版本,当我们python解释器采用3版本时,需要修改容联接口中的一些参数及方法. 首先去容联官网注册账号, ...

  10. cmdb采集数据的版本

    在局部配置文件中配置MODE=' agent',或者MODE=‘ssh’,或者MODE=‘’saltstack ',  实现只需要修改这个配置,就会使用对应的方案进行采集数据 第一种版本: 启动文件中 ...