一、资料参考
SpringBoot异步处理任务
SpringBoot整合邮件发送
SpringBoot之定时任务详解
有了这些资料便可以非常快速的实现这一功能 。
那现在就开始吧
二、添加异步任务
众所周知,发送邮件存在网络延迟问题,所以我们需要开个多线程,然后对发送邮件任务进行一个异步操作,不让用户一直处于一个等待状态(得等发送成功才能进行下一步操作),教程中有两种方法,一种是线程池,一种是注解的方法,今天将使用后者。
这种方式,是springBoot自身的一种异步方式,使用注解实现,非常方便,我们在想要异步执行的方法上加上@Async注解,在controller上加上@EnableAsync,通过之前的经验得知这个注解加载主程序上面也是可以的,这里我便将其加在主程序中。
@EnableAsync
@EnableOpenApi
@SpringBootApplication
public class SpringbootShrioApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootShrioApplication.class, args);
}
}
Service层
@Service
public class EmailServiceImpl implements EmailService {
@Override
@Async
public void sendEmail() {
}
}
自此异步任务就搭建好了,接下来是书写邮箱发送逻辑的时候了,我们只需在sendEmail中填写逻辑就好。
三、书写邮箱发送代码
导入jar包依赖
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-mail -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.4.2</version>
</dependency>
配置文件设置
mail:
host: smtp.qq.com
username: 1204082740@qq.com
password: kppmctdifiimhidj
properties.mail.smtp.ssl.enable: true
值得注意的是:最后一行配置,只有qq邮箱才需要配置,加密方式
Service层编写
@Service
public class EmailServiceImpl implements EmailService {
@Autowired
private JavaMailSender javaMailSender;
@Value("$(spring.mail.username)")
private String sendname;
@Override
@Async
/*@Scheduled(fixedRate=5000)*/
public String sendEmail(String msg, String email) {
if (StringUtils.isEmpty(msg)||StringUtils.isEmpty(email))
{
return "用户传递信息和传递对象为空值";
}
try
{
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setFrom(sendname);
simpleMailMessage.setTo(email);
simpleMailMessage.setSubject("别赖了");
simpleMailMessage.setText(msg);
javaMailSender.send(simpleMailMessage);
return "发送成功";
}
catch (Exception e)
{
return "发送失败"+e.getLocalizedMessage();
}
}
}
页面书写
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
首页
<div>
<a th:href="@{/add}">add</a>
<a th:href="@{/update}">update</a>
<div>
<form th:action="@{/sendmail}">
<input type="text" name="msg">
<input type="email" name="email">
<input type="submit" value="提交">
</form>
</div>
</div>
</body>
</html>
Controller层
@Controller
public class MailController {
@Autowired
private EmailService emailService;
@RequestMapping("/sendmail")
@ResponseBody
public String sendMail(String msg,String email)
{
String s = emailService.sendEmail(msg, email);
System.out.println(s);
return "已发送到对方邮箱";
}
}
邮件发送就书写成功了。接下来就是实现定时功能就行。
四、定时发送功能
这个定时发送也是非常简单,只需要两个注释即可
主程序
@EnableScheduling // 2.开启定时任务
@SpringBootApplication
public class SpringbootShrioApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootShrioApplication.class, args);
}
}
服务层
public class EmailServiceImpl implements EmailService {
@Autowired
private JavaMailSender javaMailSender;
@Value("$(spring.mail.username)")
private String sendname;
@Override
@Async
@Scheduled(fixedRate=5000)
public String sendEmail() {
try
{
SimpleMailMessage simpleMailMessage = new SimxxxxpleMailMessage();
simpleMailMessage.setFrom("xxx4082740@qq.com");
simpleMailMessage.setTo("x'x'x'x086851@qq.com");
simpleMailMessage.setSubject("别赖了");
simpleMailMessage.setText("试试,要能行记得跟我说喔");
javaMailSender.send(simpleMailMessage);
return "发送成功";
}
catch (Exception e)
{
return "发送失败"+e.getLocalizedMessage();
}
}
}
值得注意的是,定时功能的方法是不能带参数的,否则会报错。
还有一个多线程发送,可参考前面的链接。