邮件任务
- 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 进行邮件配置
#邮件配置
spring.mail.username=xxxxxxx@qq.com
spring.mail.password=xxxxxxx
spring.mail.host=smtp.qq.com
spring.reactor.debug-agent.enabled=true
#开启加密授权验证
spring.mail.properties.mail.smtl.ssl.enable=true
- 测试
@SpringBootTest
class TaskApplicationTests {
@Autowired
private JavaMailSenderImpl mail;
//简单的邮件
@Test
void contextLoads() {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("欢迎使用summer的文件上传接口");
message.setText("欢迎使用summer的接口,点击下方的链接进行账号激活");
message.setFrom("1260570909@qq.com");
message.setTo("1260570909@qq.com");
mail.send(message);
}
//复杂的邮件
@Test
void contextLoads2() throws MessagingException {
MimeMessage mimeMessage = mail.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setSubject("欢迎使用summer的文件上传接口");
helper.setText("点击下方的链接进行账号激活");
helper.setFrom("1260570909@qq.com");
helper.setTo("1260570909@qq.com");
helper.addAttachment("至少要见上一万次.jpg",new File("G:\\11.桌面\\Desktop\\图片\\1.jpg"));
mail.send(mimeMessage);
}
}
定时任务:
==@EnableScheduling== 开启定时任务注解
@EnableAsync //开启异步任务
@EnableScheduling //开启注解支持的定时任务
@SpringBootApplication
public class TaskApplication {
public static void main(String[] args) {
SpringApplication.run(TaskApplication.class, args);
}
}
@Scheduled(cron = "0/2 * * * * ?")
重要的就是cron表达式 cron表达式使用的时候去查询即可。
@Service
public class ScheduledService {
@Scheduled(cron = "0/2 * * * * ?")
public void hello(){
System.out.println("hello 被执行了~~~~");
}
}
开启异步任务:
==@EnableAsync== 开启异步任务的注解:
@EnableAsync //开启异步任务
@EnableScheduling //开启注解支持的定时任务
@SpringBootApplication
public class TaskApplication {
public static void main(String[] args) {
SpringApplication.run(TaskApplication.class, args);
}
}
被加上==@Async==注解的函数就会异步执行。
@Service
public class AsyncService {
@Async
public void Async(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("数据处理成功");
}
}
网友评论