SoFunction
Updated on 2025-04-04

Detailed explanation of the code for sending email reminders through scheduled tasks

summary

In actual online projects, there are continuous data sent by the pusher, and it is sent continuously. If it is suddenly interrupted, it should be a problem and the cause needs to be checked in time. This situation is more suitable for using planned tasks to check and judge, and send an email to remind you if there is a problem.

Technical details

To send emails, use spring JavaMailSender, add pom dependencies first:

        <dependency>
            <groupId></groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

Next, configure and specify the sending email address. This article uses the zoho email address:

spring:
  mail:
    host: 
    username: noreply@
    password: xxxxxx
    port: 465
    protocol: smtp
    default-encoding: utf-8
    properties:
      mail:
        smtp:
          auth: true
          starttls:
            enable: true
            required: true
          ssl:
            enable: true
          socketFactory:
            port: 465
            class: 

Then add a new method of sending emails in the service layer:

    @Autowired
    private JavaMailSender javaMailSender;
    @Override
    public void sendWarningMail(String to, String datetime) {
        String content = "xxxxHas not obtained the push data for half an hour,Test time: &lt;span style='color: red;'&gt;" + datetime + "&lt;/span&gt;。";
        MimeMessage mimeMessage = ();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
            (to);
            ("noreply@");
            (content,true);
            ("xxxx-Attention Reminder");
            (mimeMessage);
        } catch (MessagingException e) {
            (());
        }
    }

The email is sent, and then configure the scheduled task:

import ;
import ;
import ;

import ;
import ;
import ;
import ;
import .slf4j.Slf4j;
import ;
import ;
import ;
import ;
import ;

import ;

@Component
@Slf4j
@EnableScheduling
public class QSConsumer
{
    private RedisUtils redisUtils() {
        return ();//The previous blog post of SpringUtils and RedisUtils is introduced.    }
    @Autowired
    private ToutiaoPushService toutiaoPushService;

    @Async("priceExecutor")
    @Scheduled(fixedDelay = 60000) //Execute once in 1 minute    public void checkTask() {
        Date d = new Date();
        SimpleDateFormat hour = new SimpleDateFormat("HH");
        int h = ((d));
        if(h &lt; 8) return;//No checking from 12 pm to 8 pm        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long timestamp = new Date().getTime() / 1000;//Crawl data in the last half hour        List&lt;ToutiaoPush&gt; list = (timestamp - (60*30));
        if(()) {
            ("xxx@", (d));//Send to operation and maintenance            return;
        }
        ("In half an hour, we will enter the warehouse together:"+()+"Data, monitoring time:"+(d));
    }
}

Application entry class:

import ;
import ;
import ;
import ;
import ;
import ;
import ;

@SpringBootApplication
@EnableScheduling
@ServletComponentScan
@MapperScan("")
@EnableAsync(proxyTargetClass = true)//Open the asynchronous task switchpublic class PromotionApplication {

    public static void main(String[] args) {
        final ApplicationContext applicationContext = (, args);
    }
}

summary

This achieves the effect of planning task inspection, which is quite practical.

This is the introduction to this article about the detailed explanation of the code of SpringBoot sending email reminders through planning tasks. For more relevant SpringBoot sending email reminders, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!