Learn to implement scheduled email sending with Spring Boot and Resend
In a simple way, send emails through a REST API with Spring Boot and Resend.
December 23, 2024
Table of Contents
- Introduction
- Step 1: Creating the Spring Boot project
- Step 2: Implementing Resend as a dependency and configuration
- Step 3: Email service
- Step 4: Scheduler
- Step 5: Conclusion
- References
Introduction
Resend is a technology that allows you to integrate email sending in a simple and worry-free manner regarding infrastructure. In this tutorial, you will learn to implement a scheduled task in Spring Boot to send emails easily.
Step 1: Creating the Spring Boot project
To start, we need to create a Spring Boot project. For this, you can use Spring Initializr. Make sure to select the following dependencies:
- Spring Web
- Spring Boot DevTools
After that, download and unzip it in your working directory. Next, open your project in your favorite IDE or text editor; I will be using Visual Studio Code.
Step 2: Implementing Resend as a dependency and configuration
Open the pom.xml
file and add the following dependency:
It should look like this:
Step 3: Email service
We need to create a service that sends emails. To do this, create a class called EmailService
in the com.example.demo.service
package and add the following code:
As you can see, we have created a sendEmail
method that sends an email using Resend. For this, we need the Resend token, which is stored in the application.properties
file. Next, add the following properties in the application.properties
file:
You can obtain the Resend token on the Resend page. Register and get your token in the API Keys/Create API Key section, click on Create API Key.
Then, fill in the fields and click on Add
.
Finally, copy your token.
Replace <your_resend_token>
and <the_email_to_send>
with your Resend token and the email address you want to send the email to, respectively.
Now, create a META-INF
folder in resources and within it, the additional-spring-configuration-metadata.json
file with the following content:
The above is necessary for Spring Boot to recognize the properties we have defined in the application.properties
file.
Step 4: Scheduler Task
Now itโs time to create the scheduled task that runs at regular intervals. To do this, create a class called EmailScheduler
in the com.example.demo.scheduler
package and add the following code:
We have created a sendEmail
method that runs every 5 minutes. You can customize the time interval according to your needs. Additionally, we have injected the EmailService
dependency to send emails and defined the time zone as Mexico City (you can change it according to your location).
We need to add the @EnableScheduling
annotation in a configuration class to enable scheduling in Spring Boot. To do this, create a class called SchedulingConfig
in the com.example.demo.config
package and add the following code:
Now, run your Spring Boot application, and you will see that an email is sent every 5 minutes to the specified email address.
Step 5: Conclusion
Resend is a tool that integrates very easily with Spring Boot applications, is really powerful, and does not require any additional infrastructure. Personally, I have enjoyed working with Resend, and I hope you do too.
Happy coding!