Open zppkq opened 3 years ago
测试
::: hljs-center
:::
package com.hngy.zp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
//开启定时功能的注解
@EnableScheduling
//开启异步功能的注解
@EnableAsync
@SpringBootApplication
public class Day0110MailApplication {
public static void main(String[] args) {
SpringApplication.run(Day0110MailApplication.class, args);
}
}
package com.hngy.zp.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@Service
public class HelloService {
@Autowired
JavaMailSenderImpl sender;
// cron表达式
@Scheduled(cron = "* * * * * ?")
public void hello() throws MessagingException {
// 一个复杂的邮件
MimeMessage message = sender.createMimeMessage();
// 组装
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setSubject("周佩");
// 开启支持html,第二个参数
helper.setText("<p style='color:red'>测试</p>", true);
// 发送人
// 这里发送的账号必须和你配置文件里面配置的用户名一致否则会发送失败
helper.setFrom("2753176563@qq.com");
// 接受人
helper.setTo("2974590531@qq.com");
helper.addAttachment("1.jpg", new File("/Users/zhoupei/Desktop/1.jpg"));
helper.addAttachment("2.jpg", new File("/Users/zhoupei/Desktop/1.jpg"));
sender.send(message);
}
}
@zppkq
::: hljs-center
仅需两步
:::
何为定时任务:在特定的时间执行特定的任务
第一步:开启定时功能的注解
package com.hngy.zp; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.scheduling.annotation.EnableScheduling; //开启定时功能的注解 @EnableScheduling //开启异步功能的注解 @EnableAsync @SpringBootApplication public class Day0110MailApplication { public static void main(String[] args) { SpringApplication.run(Day0110MailApplication.class, args); } }
在要开启定时任务的方法上添加@Scheduled注解
package com.hngy.zp.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.MimeMessageHelper; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import javax.mail.MessagingException; import javax.mail.internet.MimeMessage; import java.io.File; @Service public class HelloService { @Autowired JavaMailSenderImpl sender; // cron表达式 @Scheduled(cron = "* * * * * ?") public void hello() throws MessagingException { // 一个复杂的邮件 MimeMessage message = sender.createMimeMessage(); // 组装 MimeMessageHelper helper = new MimeMessageHelper(message, true); helper.setSubject("周佩"); // 开启支持html,第二个参数 helper.setText("<p style='color:red'>测试</p>", true); // 发送人 // 这里发送的账号必须和你配置文件里面配置的用户名一致否则会发送失败 helper.setFrom("2753176563@qq.com"); // 接受人 helper.setTo("2974590531@qq.com"); helper.addAttachment("1.jpg", new File("/Users/zhoupei/Desktop/1.jpg")); helper.addAttachment("2.jpg", new File("/Users/zhoupei/Desktop/1.jpg")); sender.send(message); } }
dddd
http://zpsg.ltd/