1、添加Maven的发送邮件依赖。
<!--导入发送邮件的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、找到配置文件(我的是yml文件格式)文件,写上mail相关的配置信息。
spring:
mail:
default-encoding: UTF-8
host: smtp.aliyun.com
username: 邮箱账号
password: 邮箱密码
我使用的是阿里云(配置简单,过滤等级可调)邮箱,其余邮箱配置可能不一样,较为繁琐,推荐使用阿里云邮箱个人版。
3、配置阿里云邮箱。
1、 注册阿里云个人版邮箱。阿里云邮箱
2、设置垃圾等级。
4、书写发送代码。
注意事项:JavaMailSender对象必须是通过容器注入进去。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/mail")
@RestController//返回和接收的数据都是JSON类型
public class mailSend {
//JavaMailSender一定是注入的方式将对象注入
@Autowired
private JavaMailSender javaMailSender;
private String mailFor="发送给谁,邮箱是任意类型的,qq、163邮箱都可以";
private String mailFrom="由哪发出去,与yml配置文件输入的账号密码那个阿里云邮箱保持一致";
@GetMapping("/test1")
public String send(){
//创建邮箱发送对象
SimpleMailMessage message = new SimpleMailMessage();
//发给谁
message.setTo(mailFor);
//文章的标题
message.setSubject("不是垃圾邮件,看看我");
//文章的正文
message.setText("发送的内容");
//从哪发出去的
message.setFrom(mailFrom);
//发送邮件
javaMailSender.send(message);
return "执行完毕";
}
}
5、最后在浏览器输入对应的路由就可以将邮件发送出去了。
以下为SpringBoot实现异步多线程的方式实现邮件的发送
1、在上述的基础之上找到SpringBoot的主类,添加@EnableAsync注解,开启异步多线程。
2、创建线程池的配置类:
package com.example.cmos.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
@ComponentScan
//线程池配置类
@Configuration
public class ThreadPoolConfig {
@Bean("AsyncTaskExecutor")//名称不能变,表示返回的对象名称
public AsyncTaskExecutor taskExecutor(){//返回值类型不能变
//线程池对象是返回值的子类
ThreadPoolTaskExecutor executor=new ThreadPoolTaskExecutor();
// 设置核心线程数,是CPU的内核数
executor.setCorePoolSize(8);
// 设置最大线程数
executor.setMaxPoolSize(16);
// 设置队列容量
executor.setQueueCapacity(32);
// 设置线程活跃时间(秒)
executor.setKeepAliveSeconds(60);
// 设置默认线程名称
executor.setThreadNamePrefix("task-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}
3、创建线程池的任务类:
package com.example.cmos.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component
@Scope("prototype")//必须是多例模型
public class EmailTask implements Serializable {
@Autowired//发送邮件用
private JavaMailSender javaMailSender;
//由哪个邮箱发出
@Value("${cmos.email.system}")
private String mailbox;
@Async//异步
public void sendAsync(SimpleMailMessage message){
//setFrom发件人
message.setFrom(mailbox);
javaMailSender.send(message);
}
}
最后的发送部分,只有发送的对象变了,之前是由javaMailSender对象发出,现在是由上一步写的EmailTask 的对象发出
@Value("${cmos.email.hr}")
private String hrEmail;
@Autowired//发送邮件的类
private EmailTask emailTask;
public void sendEmail(){
String name="张三";
String deptName="未知部门";
String address="他家";
if (danger==1){
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(hrEmail);
//setSubject设置邮件的标题
message.setSubject("员工" + name + "身处高风险疫情地区警告");
//setText设置邮件的正文
message.setText(deptName + "员工" + name + "," +
DateUtil.format(new Date(), "yyyy年MM月dd日") + "处于"
+ address + ",属于新冠疫情高风险地区,请及时与该员工联系,核实情况!");
emailTask.sendAsync(message);
}
}