美文网首页
9.实现定时发送简单邮件

9.实现定时发送简单邮件

作者: 1只念旧的兔子 | 来源:发表于2019-04-18 00:20 被阅读0次

1.首先配置QQ邮箱->设置->账户->开启服务POP3/SMTP开启->获取授权码


2.添加pom依赖

org.springframework.boot

spring-boot-starter-mail

3.配置application.properties

spring.mail.host=smtp.qq.com

spring.mail.username=1062273622@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

4.写Service接口

publicinterfaceMailService {

/**

    * 发送简单邮件

    */

voidsendMail(Stringto,Stringsubject,Stringcontent);

}

5.实现接口

@Service("mailService")

publicclassMailServiceImplimplementsMailService{

@Autowired

privateJavaMailSender mailSender;

@Override

publicvoidsendMail(String to, String subject, String content){

SimpleMailMessage mailMessage=newSimpleMailMessage();

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("发送简单邮件失败");

        }

    }

    }

6.写定时任务:每8秒发送一份电子邮件

@Service

//@Async

publicclassTaskService{

@Autowired

privateMailService mailService;

@Scheduled(cron ="*/8 * * * *  ?")

publicvoidproces(){

mailService.sendMail("815835155@qq.com","简单邮件","lalalalalalalaal");

System.out.println("111");

    }

  }

相关文章

网友评论

      本文标题:9.实现定时发送简单邮件

      本文链接:https://www.haomeiwen.com/subject/dezowqtx.html