Learn How to Send Email Using Spring Boot!
Table of Contents
- Introduction
- Setting up the Spring Boot project
- Configuring the email properties
- Creating the EmailSenderService
- Sending a simple email
- Testing the application
- Conclusion
1. Introduction
2. Setting up the Spring Boot project
3. Configuring the email properties
4. Creating the EmailSenderService
5. Sending a simple email
6. Testing the application
7. Conclusion
How to Send a Simple Email Using Spring Boot
Sending emails programmatically is a common requirement in many applications. In this article, we will learn how to send a simple email using Spring Boot and the java mail sender library.
1. Introduction
Email communication plays a crucial role in today's digital world. Whether it's sending notifications, newsletters, or transactional emails, having the ability to send emails programmatically is essential in many applications.
In this article, we will explore how to send a simple email using the Spring Boot framework. We will leverage the java mail sender library, which provides a convenient way to send emails in Java applications.
2. Setting up the Spring Boot project
To get started, we need to set up a Spring Boot project. Follow the steps below to create a basic Spring Boot project:
- Open the Spring Initializr (https://start.spring.io/) in your web browser.
- Select Maven as the project type.
- Choose the required language (Java).
- Specify the Group and Artifact IDs for your project.
- Add the required dependencies, including the
spring-boot-starter-mail
dependency.
- Click on the Generate button to download the project structure as a ZIP file.
- Extract the downloaded ZIP file to your desired location.
3. Configuring the email properties
Once we have our Spring Boot project set up, we need to configure the properties required for sending emails. These properties include the SMTP host, port, username, password, and other optional settings.
To configure the email properties, follow the steps below:
- Open the
application.properties
file located under the src/main/resources
directory in your project.
- Add the following properties:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your-email@gmail.com
spring.mail.password=your-app-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Make sure to replace your-email@gmail.com
with your actual Gmail email address and your-app-password
with the app password generated from your Google account settings.
4. Creating the EmailSenderService
To send emails, we need to create a service class that encapsulates the logic for sending emails. Create a new Java class named EmailSenderService
in the appropriate package in your project.
Add the following code to the EmailSenderService
class:
@Service
public class EmailSenderService {
@Autowired
private JavaMailSender mailSender;
public void sendEmail(String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("your-email@gmail.com");
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
System.out.println("Mail sent successfully!");
}
}
In this code snippet, we annotate the class with @Service
to mark it as a Spring service bean. We also inject the JavaMailSender
bean provided by Spring Boot to send the email.
5. Sending a simple email
Now that we have the EmailSenderService
ready, let's go ahead and send a simple email using this service.
First, open the main class of your Spring Boot application (typically named Application
or SpringEmailDemoApplication
) and add the following code:
@SpringBootApplication
public class SpringEmailDemoApplication {
@Autowired
private EmailSenderService senderService;
public static void main(String[] args) {
SpringApplication.run(SpringEmailDemoApplication.class, args);
}
@EventListener(ApplicationReadyEvent.class)
public void sendMail() {
senderService.sendEmail("receiver-email@example.com", "Hello from Spring Boot", "This is a test email from Spring Boot.");
}
}
In this code snippet, we inject the EmailSenderService
bean using the @Autowired
annotation and call the sendEmail
method to send a test email. Replace "receiver-email@example.com"
with the actual email address where you want to receive the test email.
6. Testing the application
To test the email sending functionality, run your Spring Boot application. Once the application is running, it will automatically send an email to the specified receiver email address.
To verify the email, go to your email inbox and check for the test email with the subject "Hello from Spring Boot". Open the email to ensure that the email body matches the content provided in the code.
Congratulations! You have successfully sent a simple email using Spring Boot.
7. Conclusion
In this article, we learned how to send a simple email using Spring Boot. We explored the necessary steps to set up the Spring Boot project, configure the email properties, and created a service class for sending emails. We also tested the application by sending a test email.
Sending emails programmatically with Spring Boot opens up a wide range of possibilities for integrating email functionality into your applications. Whether it's sending notifications, password recovery emails, or transactional emails, the java mail sender library provides a convenient and straightforward way to accomplish these tasks.
By leveraging the concepts covered in this article, you can extend the email functionality to suit your specific requirements and build robust email communication features in your Spring Boot applications.