博客
关于我
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/

    你可能感兴趣的文章
    python2和python3的区别
    查看>>
    python2备份交换机配置脚本
    查看>>
    Python3 - 在字符串格式化程序参数中使用变量
    查看>>
    Python3 A*寻路算法实现
    查看>>
    python3 grpc安装
    查看>>
    python3 mysql 封装多表_python3 mysql-多表查询
    查看>>
    python3 mysql8_Python MySQL8.0 错误:authentication plugin 'calling_sha2_password' is not supported....
    查看>>
    Python关键代码总览!从Zero到Hero必看的考点
    查看>>
    python3 robotframework+pycharm框架搭建
    查看>>
    Python3 shutil模块
    查看>>
    Python关于strftime函数详细解析 附实战代码
    查看>>
    Python3 tutorial day3
    查看>>
    python3 week4
    查看>>
    Python3 创建数组
    查看>>
    python3 套接字异常(a byte-like object is required not str)
    查看>>
    python3 实现用户三次输入错误记录黑名单并对该用户登录锁定-day1
    查看>>
    python3 文本变图片
    查看>>
    Python3 查看 com 组件的属性
    查看>>
    python3 案例分享--发邮件
    查看>>
    python3 模板库_Python3 标准库概览_Python教程_我爱模板网 - 提供下载各种免费建站资源,免费网站模板,免费网页特效,让你爱上建站!...
    查看>>