• pom添加依赖:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  • properties配置

#mail
spring.mail.host=smtp.163.com
spring.mail.username=xxxx@163.com
#163的授权码
spring.mail.password=xxxx
spring.mail.default-encoding=UTF-8
  • 发送邮件工具类:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;
@Component
public class MailServiceUtils{ private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private JavaMailSender mailSender;
/** * @param from 发送人 * @param to 接收人 * @param subject 主题 * @param content 内容
*/ public void sendMail(String from,String to, String subject, String content){SimpleMailMessage message = new SimpleMailMessage(); message.setFrom(from); message.setTo(to); message.setSubject(subject); message.setText(content); try { mailSender.send(message); logger.info("邮件成功发送!"); } catch (MailException e) { logger.error("发送邮件错误:",e); } }
}
  • 启动类添加注解定时任务注解@EnableScheduling

  • 测试使用

public class Test { int i = 0;@Resourceprivate MailServiceUtils mailServiceUtils;@Scheduled(fixedRate = 4000)//定时任务4秒执行一次//@Scheduled(cron="0/4 * *  * * ? "),cron表达式,同样表示每4秒执行一次public void test(){mailServiceUtils.sendMail("from_user@163.com","to_user@qq.com","主题"+i,"你好鸭!");i++;}
}

SpringBoot定时任务(以发送邮件为例)相关推荐

  1. Springboot企业邮箱发送邮件

    1.这里以腾讯企业邮箱为例 2.首先确定springboot版本,目前使用1.5.7, 3.添加邮件依赖 <dependency><groupId>org.springfram ...

  2. SpringBoot定时任务(@Scheduled)说明

    转载文章:http://blog.csdn.net/loongshawn/article/details/50663393 1. 定时任务实现方式 定时任务实现方式: Java自带的java.util ...

  3. java 延时发送邮件_基于SpringBoot实现定时发送邮件过程解析

    前提: 1.springboot项目 2.引入maven 依赖 org.springframework.boot spring-boot-starter-mail 以下代码中涉及到的maven依赖有日 ...

  4. SpringBoot定时任务 - 集成quartz实现定时任务(单实例和分布式两种方式)

    最为常用定时任务框架是Quartz,并且Spring也集成了Quartz的框架,Quartz不仅支持单实例方式还支持分布式方式.本文主要介绍Quartz,基础的Quartz的集成案例本,以及实现基于数 ...

  5. SpringBoot定时任务@Scheduled注解详解

    SpringBoot定时任务@Scheduled注解详解 项目开发中,经常会遇到定时任务的场景,Spring提供了@Scheduled注解,方便进行定时任务的开发 概述 要使用@Scheduled注解 ...

  6. SpringBoot定时任务实现的两种方式介绍

    今天给大家介绍SpringBoot定时任务实现的几种方式,希望对大家能有所帮助! 1.SpringTask 用法 框架介绍:SpringTask是Spring自带的轻量级定时任务工具,相比于Quart ...

  7. 玩转 SpringBoot 2 之发送邮件篇

    前言 通过本文你将了解到SpringBoot 2 中发送邮件使用教程,具体详细内容如下: 发送普通的邮件 发送html格式邮件 发送html 中带图片的邮件 发送带附件的邮件 阅读前需要你必须了解如何 ...

  8. 开关配置springboot定时任务

    题外话 这是我第五篇原创文章,计划写定时任务内容,想想只要打开自己的有道云笔记,复制粘贴,整理排版一下就能轻松搞定了,这样做有意义吗?自己写文章的价值点是什么呢?考虑了很长时间,主要希望做到一下几点: ...

  9. SpringBoot使用JavaMailSender发送邮件:com.sun.mail.smtp.SMTPSendFailedException: 451 MI:SFQ 163 smtp7

    SpringBoot使用JavaMailSender发送邮件时,报错如下: 2020-08-08 07:32:21,237 ERROR --- [http-nio-8080-exec-1] cn.co ...

最新文章

  1. linux GDB详解
  2. java scala 混合编程_java与scala混合编程打包(maven构建)
  3. C语言 二维数组复制、清零及打印显示
  4. DCMTK:查询/检索服务类用户(C-GET操作)
  5. 最全面的 MySQL 索引详解
  6. MySQL高级 - 案例 - 系统性能优化分析
  7. 【NOI2013】树的计数【树的遍历】【期望】
  8. C++ 中的 #pragma warning(push) 和 #pragma warning(pop)有什么用
  9. 有关findviewbyid 一个错误用法
  10. python第十六课——ascii码
  11. rn php,RN和React路由详解及对比
  12. 51单片机跑马灯c语言,51单片机——跑马灯详解(示例代码)
  13. leetcode笔记(五)809. Expressive Words
  14. JPBC库的使用实例——BLS签名
  15. MySQL基础(补充)
  16. Codeforces div1+2
  17. PowerShell 未对文件\XXX.ps1进行数字签名。无法在当前系统上运行该脚本。
  18. 企业应尽早使用BIM技术——BIM带来的优势
  19. 微软WebCast(视频教材下载工具)上千视频下载
  20. rk3368 CPU动态调频与温度保护

热门文章

  1. 解决方案 Rect width exceeds maximum margin
  2. cursor的属性值
  3. 【软考信安】入侵检测技术原理应用
  4. Windows下的QT系统开发环境搭建
  5. 精选国外免费PHP空间推荐
  6. M1卡id号的计算算法
  7. Excel中插入柱状图以及在图下方显示数据表
  8. 铁路旅客流量预测——数据清洗
  9. Flask连接PostgreSQL数据库
  10. MFC应用程序开发教程1