spring-boot使用Scheduled定时任务

1.导入邮箱依赖

    <!--        邮件依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>

2.配置文件application.properties

#邮件发送配置
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
#邮箱号
spring.mail.username=
#授权码
spring.mail.password=
#邮件发送安全配置
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true


3.编写代码

定时发送代码块

package com.xmx.day1223_scheduled.job;import com.xmx.day1223_scheduled.util.SendMailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;
import java.util.Date;/*** @Auther: 郑兴源* @DateTime :  2021-12-23 上午 11:01* @descriptoin:定时任务的代码*/
@Component
public class MyScheduledJob {@Autowiredprivate SendMailUtil mailService;/*定时任务的方法*/@Scheduled(cron = "0/30 * * * * ?")public void scheduledMetgod() {SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println("2022定时器被触发" + sdf.format(new Date()));/*填你的测试信息*/String to = "1223614075@qq.com";String title = "郑兴源·先生被聘JD京东集团125K/月";String context = "JD京东集团中级开发人员";mailService.sendVertifyCode(to, title, context);}}

邮箱发送根据类

package com.xmx.day1223_scheduled.util;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;/*** @create: 2022-02-21 19:28* @author: 郑兴源* @description:邮件发送工具**/
@Component
public class SendMailUtil {@Value("${spring.mail.username}")private String from;@Autowiredprivate JavaMailSender mailSender;Logger logger = LoggerFactory.getLogger(this.getClass());public void sendVertifyCode(String to, String title, String content){SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from); //发送人message.setTo(to);   //收件人message.setSubject(title);  //邮件名message.setText(content);   //邮件内容(验证码)mailSender.send(message);logger.info("已经发送");}
}

运行主类上加注解

@SpringBootApplication
@EnableScheduling//必须启动定时任务

目录结构

4. /定时任务的方法表达式/@Scheduled(cron = “0/30 * * * * ?”)

格式: cron = “* * * * * ?”
[秒] [分] [小时] [日] [月] [周] [年]
序号 说明 是否必填 允许填写的值 允许的通配符 1 秒 是 0-59 , - * / 2 分 是 0-59 , - * / 3 小时 是 0-23 , - * / 4 日 是 1-31 , - * ? / L W 5 月 是 1-12 or JAN-DEC , - * / 6 周 是 1-7 or SUN-SAT , - * ? / L # 7

在线生成cron表达式

cron= 0 0 8 * * ? 表示每天早上8点

5.运行结果

spring-boot使用Scheduled定时发送邮件相关推荐

  1. Spring Boot中使用JavaMailSender发送邮件

    相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看 ...

  2. spring boot 使用 javax.mail发送邮件常见错误Authentication failed、Mail server connection failed

    最近做系统内审批业务,需要发送邮件,在本地使用公司邮箱测试时,是没有问题的,没有使用发件服务器验证: 项目使用的是spring boot 2.x; 初始配置文件: spring:mail:host: ...

  3. spring Boot手把手教学(6):发送邮件

    文章目录 1.前言 2.安装依赖 3.添加配置信息 4.代码实现 5.功能扩展 5.1.发送HTML格式邮件 5.2.发送带附件的邮件 5.3.使用`thymeleaf`模板发送邮件 6.完整代码 1 ...

  4. Spring Boot定时任务-@Scheduled的使用

    SpringBoot对于定时任务的支持,其实我们在开发过程当中,定时任务的技术使用,还是比较多的,比如说我系统当中,要求在一个特定的时间,给用户发送一些信息,或者我的系统要求在一个特定的时间,对于Re ...

  5. Spring Boot 实际应用(三)发送邮件实现

    邮件服务对于项目来说属于基础性服务了,使用spring-mail实现起来真是太简单了,但是其中还是会有一点小问题需要注意下,下面就来讲下结合spring-boot实现邮件服务的封装. 其实封装基础服务 ...

  6. Spring Boot 定时任务 Scheduled(1. fixedDelay 2. fixedRate 3. initialDelay 4. cron Scheduled)

    Api说明 fixedDelay 上一次执行完毕时间点之后多长时间再执行.如: @Scheduled(fixedDelay = 5000) //上一次执行完毕时间点之后5秒再执行 fixedRate ...

  7. Spring boot admin监控添加发送邮件

    配置文件application.yml spring:#安全配置security:user:name: adminpassword: adminboot:admin:ui:title: 服务监控中心n ...

  8. Spring Boot笔记-@Scheduled(cron=““)设置调度任务

    这个比上一篇15分钟,1分钟跑一次的要好用. 毕竟调用了框架.方便快捷,老外的东西的确好,难怪洋人在我国这么受欢迎. 下面这段来自网络: "0 0 10,14,16" * * ? ...

  9. mysql的每隔1分钟定时_简单易用,spring boot集成quartz,实现分布式定时任务

    什么是quartz? Quartz是一个完全由 Java 编写的开源任务调度框架. 我们经常会遇到一些问题: 想每个月27号,提醒信用卡还款: 想每隔1小时,提醒一下,累了,站起来活动一下: 想每个月 ...

最新文章

  1. 符号执行:利用Angr进行简单CTF逆向分析
  2. GreenDao 3.x 注解中ToOne和ToMany的个人理解
  3. hive整合sentry,impala,hue之后权限管理操作
  4. 手把手教你插入数学公式,妈妈再也不用担心我写不了论文了
  5. Hazelcast入门指南第5部分
  6. 在 520 这天,竟然有人把 Docker讲清楚了? | 原力计划
  7. MySql数据库连接种类
  8. 离职通知邮件主题写什么好_(原创)拿到了企业的offer后要注意什么?
  9. shell中单引号、双引号、反引号、反斜杠的使用
  10. c++函数可变参数的使用
  11. S3C2440移植RTL8187L无线USB网卡记录(未解决)
  12. java大文件md5快速计算_java 计算文件MD5值 大文件
  13. 新疆几十公里花海开始盛放,一株580年最老野树,开出最艳鲜花
  14. php处理证件照_如何使用ps制作证件照
  15. 电子爱好者都应该至少有一台示波器
  16. 在微信公众号中使用jquery和微信SDK
  17. 【数字信号处理】MATLAB OLA(Overlap-Add)实现变调不变速/变速不变调
  18. Oracle的基本练习:登陆、查看连接、管理登陆用户、修改进程数
  19. 二十三种设计模式-----归纳篇(JAVA)
  20. 使用IDEA画结构图

热门文章

  1. 故事的魅力—读王小波的《绿毛水怪》
  2. DIY桌面机械臂__机械结构设计_p1
  3. Linux 命令----find:查找指定的文件
  4. java实现双向RSA + AES加密
  5. CSS学习总结#【博学谷学习记录】超强总结,用心分享
  6. 一加9pro怎么样 哈苏摄像引领智能手机新潮流
  7. EasyUI中tree选中父节点自动选中子节点,取消子节点自动取消父节点,子节点勾选完毕自动勾选父节点
  8. Python相关工具使用01_设置双击直接打开.ipynb文件
  9. Android Q 基站刷新接口源码分析 适配双卡手机基站刷新逻辑
  10. React官方文档: 不使用ES6