运用SpringTask方式定时发送邮件

2018-10-25  本文已影响0人  _果不其然_
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
spring.mail.host=smtp.qq.com
spring.mail.username=2677897969@qq.com
spring.mail.password=amujxrblfdyobeeh
spring.mail.default-encoding=UTF-8
##如果不加下面3句,会报530错误
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
public interface MailService {
    /**
     * 发送简单邮件
     */
    void sendMail(String to,String subject,String content);

}
@Service("mailService")
public class MailServiceImpl implements MailService {
    @Autowired
    private JavaMailSender mailSender;

    @Override
    public void sendMail(String to, String subject, String content) {
        SimpleMailMessage mailMessage=new SimpleMailMessage();
        mailMessage.setFrom("1062273622@qq.com");//发起者
        mailMessage.setTo(to);//接受者
        mailMessage.setSubject(subject);
        mailMessage.setText(content);
        try {
            mailSender.send(mailMessage);
            System.out.println("发送简单邮件");
        }catch (Exception e){
            System.out.println("发送简单邮件失败");
        }
    }
}
@Service
//@Async
public class TaskService {
    @Autowired
    private MailService mailService;
    
    @Scheduled(cron = "0 0 1 * * ?")
    public void proces(){
        mailService.sendMail("815835155@qq.com","简单邮件","lalalalalalalaal");
        System.out.println("111");
    }
  }

cron在线生成器

上一篇下一篇

猜你喜欢

热点阅读