博客
关于我
springboot实现邮件发送,异步,定时
阅读量:321 次
发布时间:2019-03-03

本文共 2552 字,大约阅读时间需要 8 分钟。

Spring Boot邮件发送配置与异步定时任务实战

一、依赖配置

在项目中添加必要的依赖关系:

org.springframework.boot
spring-boot-starter-mail

二、邮件发送配置

1.1 配置文件说明

在application.properties中添加以下配置:

# 网易邮箱配置spring.mail.username=xxxxxx@163.comspring.mail.password=网易账号授权码spring.mail.host=smtp.163.com# 注意:使用qq邮箱时,需在配置中添加以下内容:spring.mail.properties.mail.smtp.ssl.enable=true

1.2 获取授权码

登录网易云邮箱,进入“设置” > “邮件客户端设置” > “安全中心” > “获取授权码”并开启IMAP/SMTP服务,获取授权码以完成邮箱配置。

三、邮件发送测试

3.1 简单邮件发送

在测试类中注入JavaMailSender:

@Autowiredprivate JavaMailSender mailSender;

3.1.1 发送简单邮件

@Testpublic void contextLoads() {    SimpleMailMessage simpleMailMessage = new SimpleMailMessage();    simpleMailMessage.setSubject("主题");    simpleMailMessage.setText("邮件内容");    simpleMailMessage.setTo("收件人邮箱");    simpleMailMessage.setFrom("发件人邮箱");    mailSender.send(simpleMailMessage);}

3.2 复杂邮件发送

@Testpublic void contextLoads2() throws MessagingException {    MimeMessage mimeMessage = mailSender.createMimeMessage();    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);    helper.setSubject("主题");    helper.setText("支持HTML样式", true);    helper.addAttachment("1.jpg", new File("C:\\Users\\lenovo\\Desktop\\1.jpg"));    helper.setTo("收件人邮箱");    helper.setFrom("发件人邮箱");    mailSender.send(mimeMessage);}

四、异步处理

4.1 开启异步功能

在主程序中添加注解:

@EnableAsync@SpringBootApplicationpublic class Springboot11TestApplication {    public static void main(String[] args) {        SpringApplication.run(Springboot11TestApplication.class, args);    }}

4.2 异步业务逻辑

@Servicepublic class AsyncService {    @Async    public void hello() {        try {            Thread.sleep(3000);        } catch (InterruptedException e) {            e.printStackTrace();        }        System.out.println("数据正在处理中");    }}

五、定时任务

5.1 开启定时任务

在主配置类中添加注解:

@EnableScheduling@SpringBootApplicationpublic class Springboot11TestApplication {    public static void main(String[] args) {        SpringApplication.run(Springboot11TestApplication.class, args);    }}

5.2 定时任务配置

@Servicepublic class ScheduledService {    @Scheduled(cron = "50 30 15 * * ?") // 每天的0秒任何时候都    public void hello() {        System.out.println("hello,你被执行了");    }}

六、常用cron表达式示例

cron表达式是用空格分隔的字段,每个字段代表不同的时间单位,具体含义如下:

  • 秒:0-59
  • 分钟:0-59
  • 小时:0-23
  • 天:0-31(31代表每月最后一天)
  • 月:1-12(1代表一月,12代表十二月)
  • 周几:1-7(1代表星期一,7代表星期日)

以下是常见的cron表达式示例:

  • 每2秒执行一次:
  • 0/2 * * * * ?
    1. 每2分钟执行一次:
    2. 0 0/2 * * * ?
      1. 每月的1日凌晨2点:
      2. 0 2 1 * * ?
        1. 周一到周五每天上午10:15:
        2. 0 15 10 ? * MON-FRI
          1. 2002年至2006年的每月最后一个星期五上午10:15:
          2. 0 15 10 ? 6L 2002-2006
            1. 每月的第三个星期五上午10:15:
            2. 0 15 10 ? * 6#3

              通过合理设计cron表达式,可以根据具体需求定时触发业务逻辑。

    转载地址:http://qfsm.baihongyu.com/

    你可能感兴趣的文章
    Python与Excel的完美邂逅:深入浅出,手把手教你轻松玩转Excel数据操作
    查看>>
    python与flask_Python编程中flask的简介与简单使用
    查看>>
    python与图灵机器人交互(WXPY版本)
    查看>>
    python与运算符_python运算符
    查看>>
    python两个列表获取交集,并集,差集
    查看>>
    python两个字典合并,两个list合并
    查看>>
    Python两个版本共存时,命令行升级pip
    查看>>
    Python两个版本共存时,怎么验证是否安装成功 scrapy
    查看>>
    Python两个版本共存时,通过pip安装Scrapy
    查看>>
    python两组变量的相关关系散点图_变量是什么?变量间的两种关系——函数关系和相关关系(小学版)...
    查看>>
    Python中 pass语句的作用
    查看>>
    python中 urllib, urllib2, httplib, httplib2 几个库的区别
    查看>>
    python中 yaml库 & json库 & xmltodict库使用
    查看>>
    python中 将你的名字转化成为二进制并输出
    查看>>
    Python中*args 和**kwargs的用法
    查看>>
    Python中*args 和**kwargs的用法
    查看>>
    Python中*args和**kwargs的使用
    查看>>
    python中0、1、True、False、if条件
    查看>>
    python中argparse模块关于 parse_args() 函数详解(全)
    查看>>
    python中base64编码与解码
    查看>>