概念
定时计划任务功能在Java中主要使用的就是Timer对象,它在内部使用多线程的方式进行处理,所以它和多线程技术还是有非常大的关联的。在JDK中Timer类主要负责计划任务的功能,也就是在指定的时间开始执行某一个任务,但封装任务的类却是TimerTask类。
通过继承 TimerTask 类 并实现 run() 方法来自定义要执行的任务置计划任务,而封装任务内容的类是TimerTask类.此类是一个抽象类,继承需要实现一个run方法
邮件发送
首先我们先实现邮件发送,这里我使用的是QQ邮箱,具体步骤如下:
1:在QQ邮箱打开SMTP服务,具体步骤如下
1.1:打开服务,发送短信获取授权码
1.2:一定要拿这个授权码,等下是有用的
2.这里我是使用的Springboot项目,所以可以直接导入如下依赖即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
3.配置QQ邮箱的配置文件(properties)
#配置邮件消息
spring.mail.host=smtp.qq.com
#发送邮件者信箱
spring.mail.username=1301178858@qq.com
#PO3/SMTP服务时邮箱的授权码
spring.mail.password=uzoxibznltivhhai
spring.mail.default-encoding=UTF-8
spring.mail.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.debug=true
4.编码
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
/**
* @Author uncletj
* @Date 2021/5/27 16:43
* @Version SpringBoot 2.2.2
* @projectName 藤井大叔
*/
@SpringBootTest
public class EmailTest {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from = null;
@Test
void emilaTest(){
SimpleMailMessage message = new SimpleMailMessage();
//1.发送邮件人
message.setFrom(from);
//2.收信人
message.setTo("1301178858@qq.com");
//3.标题
message.setSubject("【今日提醒】");
message.setText("今天记得吃早餐、午餐、晚餐");
mailSender.send(message);
}
}
下面来看结果
既然邮箱是已经收到了,那么就说明事已经做一半,下面我们在实现自己定义时间来进行邮件发送
定时发送
具体实现如下,时间是我自己定义的
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* @Author uncletj
* @Date 2021/5/27 17:08
* @Version SpringBoot 2.2.2
* @projectName 藤井大叔
*/
@RestController
public class EmailController {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from = null;
@PostMapping("/email/send")
public String getByEmailSend() throws ParseException {
SimpleMailMessage message = new SimpleMailMessage();
String email_date = "2021-05-27 17:20:00";
SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date =new Date();
System.out.println("准备发送,现在时间为------------>"+sf.format(date));
//定时主要实现就是这个,实现run方法
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//1.发送邮件人
message.setFrom(from);
//2.收信人
message.setTo("1301178858@qq.com");
//3.标题
message.setSubject("【今日提醒】");
message.setText("今天记得吃早餐、午餐、晚餐");
mailSender.send(message);
System.out.println("邮件收到时间为----------->"+email_date);
}
},new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(email_date));
return "SSUCCESS";
}
}
测试
启动后的控制台是这样的,只有一条输出语句,也就证明该方法是已经成功被调用了,调用时间是在2021-05-27 17:18:51,而我想到在2021-05-27 17:20:00分收到此邮件信息
在2021-05-27 17:20:00的时候此邮件被调用,输出提示语句也被执行,证明邮件是发送成功了
这时候我们收到的QQ邮箱也是与收件时间相同的
【不要认为自己很强,也不要以为自己很弱,迷茫不是归宿】