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.
- Refer Sending Email Using Spring Framework Example to get more details about Spring email integration.
Spring Quartz integration facilitates the use of Quartz for job scheduling.
- Refer Spring Integration With Quartz Scheduler to get more details about Spring integration support for Quartz.
Technologies Used
- Spring 5.0.8.Release
- Java 10
- Java Mail API
- Quartz Scheduler
- Maven dependencies
- Spring email scheduling example using Quartz scheduler
- Bean class
- Spring email scheduling example – Quartz Job class
- Spring email scheduling example – Mail Service class
- Scheduling Configuration
Maven Dependencies
You need to add following dependencies for sending mail using Quartz scheduler in Spring framework.
UTF-8 5.0.8.RELEASE junit
junit3.8.1 test org.springframework
spring-core${spring.version} org.springframework
spring-context${spring.version} org.springframework
spring-context-support${spring.version} org.springframework
spring-tx${spring.version} com.sun.mail
javax.mail1.6.1 javax.mail
javax.mail-api1.6.1 org.quartz-scheduler
quartz2.3.0 javax.activation
activation1.1.1
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 (User.java)
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;
this.email = 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) {
this.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.
@Service
public class MailReminderJob extends QuartzJobBean {
private EmailService emailService;
public void setEmailService(EmailService emailService) {
this.emailService = emailService;
}
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
System.out.println("In executeInternal");
ListuserList = getListOfUsers();
for(User user : userList) {
emailService.sendMail(user);
}
}
// Dummy method for adding List of Users
private ListgetListOfUsers() {
Listusers = new ArrayList ();
users.add(new User("Jack", "Reacher", "[email protected]"));
users.add(new User("Remington", "Steele", "[email protected]"));
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 email.properties which is stored in the application’s classpath. MimeMessageHelper is used here so that attachment can also be sent with the email.
EmailService.java
@Service("emailService")
@Configuration
@PropertySource("classpath:email.properties")
public class EmailService {
@Autowired
private Environment env;
@Autowired
private JavaMailSender mailSender;
public void sendMail(User user) {
System.out.println("In Send mail");
try{
MimeMessage message = mailSender.createMimeMessage();
// use the true flag to indicate you need a multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(env.getProperty("email.From"));
// each user's email ID
helper.setTo(user.getEmail());
helper.setSubject(env.getProperty("email.Subject"));
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"));
this.mailSender.send(message);
}
catch (MessagingException ex) {
// just printing
System.out.println(ex.getMessage());
}
}
}
email.properties
[email protected]
[email protected]
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.
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">smtp true true true
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
You may also like-