Sending Mail from SpringBoot Application

Sending Mail from SpringBoot Application

Introduction

In modern applications, sending emails is a common requirement, whether it’s for user registration, password recovery, or notifications. Spring Boot provides robust support for sending emails via its JavaMailSender interface. Coupled with Mailtrap, a testing SMTP server, developers can easily test email functionalities without sending actual emails to users.

This article will guide you through the process of setting up a Spring Boot application to send emails using JavaMailSender and Mailtrap.

Pre-requisites

a. JDK 8+

b. Spring Boot 2.5+

c. Maven

d. An active Mailtrap account ( You can sign up for free from MailTrap )

Step 1: Setting up the Spring Boot Project

Create a Spring Boot Project using Spring Initializer. Give details such as project, language, SpringBoot version and project metadata.

Add necessary dependencies on pom.xml

Step 2: Configuring Mailtrap SMTP Settings

Mailtrap provides a safe environment to test your email functionality. The next step is to configure the SMTP settings provided by Mailtrap in your application.properties file.

Obtain SMTP Credentials from Mailtrap:

  • Login into active MailTrap account

  • Navigate to Sending Domains sections. Here by default domain credentials we can use or can create custom domain.

  • Click on domain to get SMTP credentials.

Replace username and password with actual MailTrap credentials.

Step 3: Implementing the Email Service

Now, we’ll create a service that leverages JavaMailSender to send emails.

Let's deep dive into below main class to understand the fundamentals.

a. @SpringBootApplication annotation defines the main class of the Spring Boot application.

b. The main method is the entry point for the Spring Boot application. It calls SpringApplication.run(...) to launch the application.

c. @Autowired This line injects an instance of the EmailService class into the senderService field.

d. sendMail() method is annotated with @EventListener, which tells Spring to execute the method when the specified event occurs.

e. The method sendMail is called on the senderService object, sending an email to "*****@gmail.com" with the subject "Test Email from SpringBoot" and body "This is body of Email".

This means that as soon as the Spring Boot application is up and running, it will automatically send an email with the specified subject and body to the provided recipient address.

Step 4: Testing the Email Functionality

To test the email-sending functionality:

Run our Spring Boot application by running the main class from your IDE.

As we can see from above screenshot "Mail sent successfullyy.." message shown in the logs.

One your Email which you have configure in toEmail section. It will appear like this.

Conclusion:

In this article, we covered how to send emails using Spring Boot and JavaMailSender, using Mailtrap for testing. We discussed configuring SMTP settings and creating a simple email service.