Popular searches
//

Email sending with Spring Batch

6.9.2011 | 3 minutes reading time

Like in the last post (Send data secure with sftp and Spring Batch ) this post is handling a not very new but also very important technology. It shows, how sending emails can be handled easily with Spring Batch. This framework has some ready components, which only must be combined.

In the following example three beans are declared, which are needed for sending emails:

  • The JavaMail Bean and set all needed parameters
  • The Tasklet Bean, which calls the service for sending the mail. This bean is the central bean and reference both other beans.
  • The Service bean

The Java Mail Bean

The Tasklet Bean

The Service Bean

Now these three beans can be called by a BatchJob.

Now you have to create the needed Java class files wich are called by the beans in XML. The Bean “sendMailService” is a reference to the de.batch.mail.SendMailService class. In the BatchJob it is called by the Tasklet named “sendMailManager”.

This is the tasklet, which calls the Service:

Now you are able to send emails by your BatchJob with a CronJob.