栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot怎样使用多线程异步发送邮件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot怎样使用多线程异步发送邮件

一、引入邮件发送依赖


     org.springframework.boot
     spring-boot-starter-mail

二、配置文件

#QQ邮箱服务器
spring.mail.host=smtp.qq.com
#端口号
spring.mail.port=587
#你的QQ邮箱账户
spring.mail.username=你的QQ号@qq.com
#你的QQ邮箱第三方授权码
spring.mail.password=你的QQ邮箱第三方授权码
#编码类型
spring.mail.default-encoding=UTF-8

三、自定义@Async线程池

package com.trade.market.Thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync//开启异步功能
public class BaseAsyncConfigurer implements AsyncConfigurer {

    private static final Logger LOG = LoggerFactory.getLogger(BaseAsyncConfigurer.class);
    
    @Override
    public Executor getAsyncExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(20);
        executor.setQueueCapacity(200);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("taskExecutor-");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();//初始化
        return executor;
    }

    
    @Override
    public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
        return (Throwable ex, Method method, Object... params)->{
            //todo 异步方法异常处理
            LOG.error("class#method: {}" ,method.getDeclaringClass().getName() ,"#{}" ,method.getName());
            LOG.error("type        : {}" , ex.getClass().getName());
            LOG.error("exception   : {}" , ex.getMessage());
        };
    }

}

四、定义邮件发送工具类

package com.trade.market.Thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class SendEmail{
    @Resource
    private JavaMailSenderImpl mailSenderl;
    @Value("${spring.mail.username}")
    private String from;

    private static final Logger LOG = LoggerFactory.getLogger(SendEmail.class);

    @Async
    public void sendSimpleEmail(String to,String subject,String text){
        SimpleMailMessage message=new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(text);
//        发送邮件
        mailSenderl.send(message);
        LOG.info("邮件发送结果:发送成功!");
    }
}

五、使用方法

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1013668.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号