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. 创建定时任务
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天天进步2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值