java mail 多线程_SpringBoot之mail发邮件和多线程

前言:我之前用Log4j发邮件,发现特别影响性能,如果频繁遇到异常触发邮件就会影响其他请求访问,后来换成 new thread 来单独 发送 javaXMail 。

今天发现 spring-boot-starter-mail,虽然也是依赖javaxMail 实现的,但是强迫症犯了 ,认为Spring大法是最好的。

1.maven依赖

org.springframework.boot

spring-boot-starter-mail

2.配置相关配置

#mail

spring.mail.host=smtp.exmail.qq.com

spring.mail.username=

spring.mail.password=

spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true

spring.mail.sendTo=

3.使用方法

@Autowired

private JavaMailSender mailSender;

SimpleMailMessage message = new SimpleMailMessage();

message.setFrom(env.getProperty("spring.mail.username"));

message.setTo(env.getProperty("spring.mail.sendTo"));

message.setSubject(title);

message.setText(text);

mailSender.send(message);

测试发现用JavaMailSender 发送也是同步,影响性能

4.使用Spring的TaskExecutor实现异步发送。

@Bean

public ThreadPoolTaskExecutor createThreadPoolTaskExecutor() {

ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();

threadPoolTaskExecutor.setCorePoolSize(10);

threadPoolTaskExecutor.setMaxPoolSize(20);

return threadPoolTaskExecutor;

}

taskExecutor.execute(() -> {

try {

SimpleMailMessage message = new SimpleMailMessage();

message.setFrom(env.getProperty("spring.mail.username"));

message.setTo(env.getProperty("spring.mail.sendTo"));

message.setSubject(title);

message.setText(text);

mailSender.send(message);

} catch (Exception e) {

logger.warn("发送邮件消息失败=exception:{}", e);

}

});

上面的()->的写法是 java8的Lambda写法, new Runnable()

5.使用Spring的@Async实现异步

@Async

public static void AsyncMethod(){

//执行发邮件操作

}

需要启用Async , 配置一下@EnableAsync

通过@Async注解表明该方法是异步方法,如果注解在类级别上则表明该类所有的方法都是异步方法。

java原生也有线程池,有兴趣的同学可以去看看ThreadPoolExecutor类的说明

6.Main方法运行

// 创建Properties 类用于记录邮箱的一些属性

final Properties props = new Properties();

// 表示SMTP发送邮件,必须进行身份验证

props.put("mail.smtp.auth", "true");

//此处填写SMTP服务器

props.put("mail.smtp.host", "smtp.qq.com");

//端口号,QQ邮箱给出了两个端口,但是另一个我一直使用不了,所以就给出这一个587

props.put("mail.smtp.port", "587");

// 此处填写你的账号

props.put("mail.user", "2251181679@qq.com");

// 此处的密码就是前面说的16位STMP口令

props.put("mail.password", "");

// 构建授权信息,用于进行SMTP进行身份验证

Authenticator authenticator = new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

// 用户名、密码

String userName = props.getProperty("mail.user");

String password = props.getProperty("mail.password");

return new PasswordAuthentication(userName, password);

}

};

// 使用环境属性和授权信息,创建邮件会话

Session mailSession = Session.getInstance(props, authenticator);

// 创建邮件消息

MimeMessage message = new MimeMessage(mailSession);

// 设置发件人

InternetAddress form = new InternetAddress(props.getProperty("mail.user"));

message.setFrom(form);

// 设置收件人的邮箱

InternetAddress[] sendTo = new InternetAddress[2];

sendTo[0] = new InternetAddress("22511181167911@qq.com");

sendTo[1] = new InternetAddress("1431081761916@qq.com");

message.setRecipients(Message.RecipientType.TO, sendTo);

// 设置邮件标题

message.setSubject("测试邮件");

// 设置邮件的内容体

message.setContent("这是一封测试邮件", "text/html;charset=UTF-8");

// 最后当然就是发送邮件啦

Transport.send(message);

博客地址:http://my.oschina.net/wangnian

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值