SpringBoot三种任务
作者:互联网
任务
- 异步任务
启动类加上@EnableAsync注解
@EnableAsync
@SpringBootApplication
public class Springboot10MessionyibuApplication {
public static void main(String[] args) {
SpringApplication.run (Springboot10MessionyibuApplication.class, args);
}
}
将方法标注为异步方法 @Async
@Service
public class AsyncService {
//告诉spring这是一个异步的方法
@Async
public void hello(){
try {
Thread.sleep (3000);
} catch (InterruptedException e) {
e.printStackTrace ();
}
System.out.println ("数据正在处理。。。");
}
}
-
定时任务
-
邮件任务
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
开启qq邮箱中Smtp服务
配置文件
spring.mail.username=xxxxxxxxx@qq.com
spring.mail.password=xxxxxxxxxxxx
spring.mail.host=smtp.qq.com
#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true
编写内容测试
//一般放到Controller或着封装为一个方法
@SpringBootTest
class Springboot10MessionyibuApplicationTests {
@Autowired
JavaMailSender javaMailSender;
@Test
//简单邮件发送
void contextLoads() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage ();
simpleMailMessage.setSubject ("jsp 你好");
simpleMailMessage.setText ("谢谢");
simpleMailMessage.setTo ("xxxxxxxxx@qq.com");
simpleMailMessage.setFrom ("xxxxxxxxx@qq.com");
javaMailSender.send (simpleMailMessage);
}
@Test
//复杂邮件发送
void contextLoads1() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage ();
//组装
MimeMessageHelper helper = new MimeMessageHelper (mimeMessage,true);
helper.setSubject ("slla");
helper.setText ("<p>xxxx<p>",true);//html样式
helper.setTo ("xxxxxxxxx@qq.com");
helper.setFrom ("xxxxxxxxx@qq.com");
//附件
helper.addAttachment ("1.jpg",new File ("C:\\Users\\Administrator\\Desktop\\1.jpg"));
javaMailSender.send (mimeMessage);
}
//封装为一个工具类
/**
* @Param html
* @Param subject
* @throws MessagingException
* @Author Liuyunsan
* */
public void sendMail(Boolean html, String subject,String text,String to,String from,String Filename,String Filepath) throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage ();
//组装
MimeMessageHelper helper = new MimeMessageHelper (mimeMessage,true);
helper.setSubject (subject);
helper.setText (text,html);//html样式
helper.setTo (to);
helper.setFrom (from);
//附件
helper.addAttachment (Filename,new File (Filepath));
javaMailSender.send (mimeMessage);
}
}
标签:qq,SpringBoot,helper,simpleMailMessage,任务,三种,mail,mimeMessage,String 来源: https://www.cnblogs.com/Liuyunsan/p/15646194.html