java 异步任务,定时任务,邮件
-
异步任务
- 异步方法注解: @Async
- 主程序开启异步注解功能:@EnableAsync
-
定时任务
- 开启定时功能的注解在main: @EnableScheduling
TaskScheduler :任务调用者 TaskExecutor: 任务执行者 @EnableScheduling 开启定时功能的注解 @Scheduled 固定时间执行
-
邮件发送: 简单邮件发送
- pom.xml 添加启动器依赖: spring-boot starter-mail
- 代码使用JavaMailSenderImpl 发送邮件
@Autowired JavaMailSenderImpl mailSender; pubiic void SendEmail(){ SimpleMailMessage mailMessage=new SimpleMailMessage(); mailMessage.setSubject("邮件标题"); mailMessage.setText("邮件正文"); mailMessage.setTo("[email protected]"); mailMessage.setFrom("[email protected]"); mailSender.send(mailMessage); }
4.复杂邮件发送: 字体颜色,附件
@Autowired
JavaMailSenderImpl mailSender;
pubiic void SendEmail(){
//复杂的邮件
MimeMessage mailMessage=mailSender.createMimeMessage
//组装
MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,Multipart:true);
//正文
helper.setSubject("邮件标题2");
helper.setText("<p style='color:red'>邮件正文</p>",html:true);
//附件
helper.addAttachment("1.jpg",new File("c:\\users\\administrator\\desktop\\1.jpg"));
helper.setTo("[email protected]");
helper.setFrom("[email protected]");
mailSender.send(mailMessage);
}
标签:异步,java,helper,mailMessage,任务,mailSender,邮件
From: https://www.cnblogs.com/chenshaojun2008/p/18148172