当前位置: 首页 > article >正文

SpringBoot实现定时发送邮件功能

一、项目介绍

在实际开发中,我们经常需要实现定时发送邮件的功能,比如每天发送日报、每周发送周报等。本文将介绍如何使用SpringBoot实现定时发送邮件的功能。

二、环境准备

  • JDK 1.8+
  • SpringBoot 2.x
  • Maven

三、项目配置

1. 添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2. 配置application.yml

spring:
  mail:
    host: smtp.qq.com
    username: your-email@qq.com
    password: your-smtp-password
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true

四、代码实现

1. 创建邮件服务类

@Service
public class EmailService {
    @Autowired
    private JavaMailSender mailSender;
    
    public void sendEmail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom("your-email@qq.com");
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }
}

2. 创建定时任务

@Component
public class EmailScheduleTask {
    @Autowired
    private EmailService emailService;
    
    // 每天早上9点执行
    @Scheduled(cron = "0 0 9 * * ?")
    public void sendDailyEmail() {
        String to = "recipient@example.com";
        String subject = "每日提醒";
        String content = "这是一封定时发送的邮件";
        emailService.sendEmail(to, subject, content);
    }
}

3. 启用定时任务

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

五、Cron表达式说明

常用的Cron表达式格式:

  • 0 0 9 * * ? : 每天早上9点
  • 0 0 */1 * * ? : 每小时执行一次
  • 0 0/30 * * * ? : 每30分钟执行一次
  • 0 0 9 ? * MON : 每周一早上9点

六、注意事项

  1. 使用QQ邮箱需要开启SMTP服务并获取授权码
  2. 确保服务器时间准确
  3. 建议添加异常处理和日志记录
  4. 可以使用异步方式发送邮件,避免影响主流程

七、进阶功能

  1. 发送HTML格式邮件
public void sendHtmlEmail(String to, String subject, String content) {
    MimeMessage message = mailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("your-email@qq.com");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content, true);
        mailSender.send(message);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}
  1. 添加附件
public void sendAttachmentEmail(String to, String subject, String content, String filePath) {
    MimeMessage message = mailSender.createMimeMessage();
    try {
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setFrom("your-email@qq.com");
        helper.setTo(to);
        helper.setSubject(subject);
        helper.setText(content);
        FileSystemResource file = new FileSystemResource(new File(filePath));
        helper.addAttachment(file.getFilename(), file);
        mailSender.send(message);
    } catch (MessagingException e) {
        e.printStackTrace();
    }
}

八、总结

通过SpringBoot实现定时发送邮件功能非常简单,只需要几个步骤:

  1. 添加相关依赖
  2. 配置邮件服务器信息
  3. 实现发送邮件的服务
  4. 创建定时任务

http://www.kler.cn/a/441335.html

相关文章:

  • 【GoF23种设计模式】02_单例模式(Singleton Pattern)
  • 复原IP地址 什么是运算符重载? 如何在 C++ 中进行运算符重载?运算符重载在面向对象编程中的好处是什么?getline方法
  • 爬虫基础学习
  • 同轴共桨单兵微型无人机技术详解
  • 机器学习-多元线性回归
  • C# 入门编程
  • 使用C#获取有关磁盘驱动器的信息例如大小和可用空间
  • V少JS基础班之第二弹
  • 【基础】nginx简单配置
  • leetcode--mysql
  • HTMLCSS:3D卡片翻转悬停效果
  • 运维报错:SALV报错 CX_SALV_OBJECT_NOT_FOUND
  • PHP8.4下webman直接使用topthink/think-orm
  • 12.17双向链表,循环链表
  • LeetCode - #163 缺失的区间(会员题)
  • Base64在线编码解码 - 加菲工具
  • AngularJS 与 SQL 的集成应用
  • Electron-Vue 主进程远程调试 Webpack/Webstorm 打包配置细节记录
  • 后端-使用redis缓存菜品分类数据
  • 基于python快速部署属于你自己的页面智能助手