How to send bulk emails using Spring Batch
Imagine you had an e-commerce application where you’re implementing microservices architecture. Well, one of those microservices could be an email sending application where its only role is to send emails to the customers.
In this example, we’ll be sending emails to all customers who have placed an order which has been confirmed and is ready for shipping.
We’ll take a look at how to send 500+ emails and how to configure a batch job, add a step and configure a reader, processor, and writer. You can find all the code in this git repository together with a test database: https://github.com/mamin11/batch_email_sender
I’ll be using MailHog as an email server. It’s free and easy to set up. I have mine running on docker. You can use this link to find more information on how to set up yours. https://hub.docker.com/r/mailhog/mailhog/
Create project
The first thing we’ll need to do is head over to https://start.spring.io/ and create a new application. We’ll need the following dependencies:
- Spring batch
- MySQL driver
- spring data JPA
- java mail sender
- Lombok — optional
Once you’ve done the above, download your application and open it in IntelliJ