Thursday, November 18, 2021

Spring Email Scheduling Example Using Quartz Scheduler

In this post we’ll see a Spring email scheduling example using Quartz scheduler integration with Spring framework.

Spring Mail API is used to configure Java mail for sending email.

Spring Quartz integration facilitates the use of Quartz for job scheduling.

Technologies Used

  • Spring 5.0.8.Release
  • Java 10
  • Java Mail API
  • Quartz Scheduler

Maven Dependencies

You need to add following dependencies for sending mail using Quartz scheduler in Spring framework.


  <!-- Spring module Required for Quartz integration -->


Spring email scheduling example using Quartz scheduler

In this Spring email scheduling example reminder mail is sent to a set of Users everyday at the scheduled time. For scheduling the mail to be triggered at the specific time Quartz scheduler is used.

Bean class (

public class User {

  private String firstName;
  private String lastName;
  private String email;

  public User() {
  public User(String firstName, String lastName, String email) {
   this.firstName = firstName;
   this.lastName = lastName; = email;
  public String getFirstName() {
   return firstName;
  public void setFirstName(String firstName) {
   this.firstName = firstName;
  public String getLastName() {
   return lastName;
  public void setLastName(String lastName) {
   this.lastName = lastName;
  public String getEmail() {
   return email;
  public void setEmail(String email) { = email;

Spring email scheduling example – Quartz Job class

Quartz scheduler job that would be called at the configured time. This class uses the EmailService class for sending the email. Here a dummy list of Users is created and EmailService method is called for each user in the list once the job is triggered.

public class MailReminderJob extends QuartzJobBean {
  private EmailService emailService;
  public void setEmailService(EmailService emailService) {
    this.emailService = emailService;
  protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
    System.out.println("In executeInternal");
    List<User> userList = getListOfUsers();
    for(User user : userList) {
  // Dummy method for adding List of Users
  private List<User> getListOfUsers() {
    List<User> users = new ArrayList<User>();
    users.add(new User("Jack", "Reacher", ""));
    users.add(new User("Remington", "Steele", ""));  
    return users;

Spring email scheduling example – Mail Service class

Class used for sending the mails. This class get the mail properties like From and Subject from a properties file which is stored in the application’s classpath. MimeMessageHelper is used here so that attachment can also be sent with the email.

public class EmailService {
 private Environment env;
 private JavaMailSender mailSender;

 public void sendMail(User user) {
  System.out.println("In Send mail");
   MimeMessage message = mailSender.createMimeMessage();
   // use the true flag to indicate you need a multipart message
   MimeMessageHelper helper = new MimeMessageHelper(message, true);
   // each user's email ID
   helper.setText("Dear " + user.getFirstName() + " " +user.getLastName()
   + "\r\n" + "This is a reminder mail to go through the attached PDF.");
   helper.addInline("Inline image", new ClassPathResource("netjs.png"));
   helper.addAttachment("MyImage.png", new ClassPathResource("netjs.png"));
   helper.addAttachment("MyPdf.pdf", new FileSystemResource("F:\\Doc\\index.pdf"));
  catch (MessagingException ex) {
   // just printing
email.Subject=Reminder Mail

Spring email scheduling example – Scheduling Configuration

The job is configured here to run at 8 AM everyday.

CronTrigger is used here for triggering the job using the implementation CronTriggerFactoryBean provided by the Spring framework for Quartz scheduling.

Trigger is scheduled using a SchedulerFactoryBean that exposes triggers to be set as properties.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
  <context:component-scan base-package="org.netjs.service" />
  <!-- Mail related configuration -->
  <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="host" value=""/>
    <property name="port" value="587"/>
    <property name="username" value="GMAIL_USER_NAME"/>
    <property name="password" value="PASSWORD"/>
    <property name="javaMailProperties">
        <prop key="mail.transport.protocol">smtp</prop>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
        <prop key="mail.debug">true</prop>
  <!-- Quartz scheduler related configuration -->
  <bean name="emailReminderJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
    <property name="jobClass" value="org.netjs.service.MailReminderJob"/>
    <property name="jobDataAsMap">
        <entry key="emailService" value-ref="emailService"></entry>

  <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
    <property name="jobDetail" ref="emailReminderJob"/>
    <!-- run every morning at 8 AM -->
    <property name="cronExpression" value="0 0 8 * * ?"/>
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <property name="triggers">
        <ref bean="cronTrigger"/>

To run this example you can use the following class.

public class App {
  public static void main( String[] args ){
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");


That's all for this topic Spring Email Scheduling Example Using Quartz Scheduler. If you have any doubt or any suggestions to make please drop a comment. Thanks!

>>>Return to Spring Tutorial Page

Related Topics

  1. Spring MVC JSON as Response Example
  2. Spring Transaction Management Example - @Transactional Annotation and JDBC
  3. Spring Batch Processing Using JDBCTemplate batchUpdate() Method
  4. Spring Component Scan Example
  5. How to Read Properties File in Spring Framework

You may also like-

  1. Run Time Injection Using Spring Expression Language(SpEL)
  2. Excluding Bean From Autowiring in Spring
  3. BeanFactoryPostProcessor in Spring Framework
  4. Spring Web MVC Tutorial
  5. Difference Between Comparable and Comparator in Java
  6. How to Display Time in AM-PM Format in Java
  7. Java Collections Interview Questions And Answers
  8. Tips For Improving MapReduce Performance


  1. How to use JavaClasss instead of ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("appcontext.xml");

    1. You can use this post Spring JavaConfig Example Program as reference. It is easy actually here Class is org.springframework.scheduling.quartz.JobDetailFactoryBean that means create instance of this class using new then use setters to set properties.