Sending Emails in Spring Boot via SMTP

  • 4.1/5
  • 5556
  • Jul 20, 2024

To send an email in a Spring Boot application, you can make use of the JavaMailSender provided by Spring Framework.

1) Add Dependencies

Make sure you have the necessary dependencies in your pom.xml file:

2) Get app password from Gmail account

To create and use app passwords from a Gmail account, follow these steps:

1) Make sure you are logged in to the Gmail account for which you want to create an app password.

2) Click on your profile picture or initial in the top-right corner of the Gmail interface, then click on "Manage your Google Account." This will take you to your Google Account settings.

3) In your Google Account settings, find and click on the "Security" tab on the left-hand side.

4) If you haven't already set up 2-Step Verification for your account, you will need to enable it. This provides an extra layer of security for your account. Follow the on-screen instructions to set up 2-Step Verification using your preferred method (e.g., phone number, Authenticator app).

5) After enabling 2-Step Verification, scroll down to the "Signing in to Google" section in the Security settings. Here, you will find the "2-Step Verification" option. Click on it.

6) At the bottom of the "2-Step Verification" page, select App passwords. Enter a name that helps you remember where you'll use the app password. Select Generate.

7) To generate the app password, follow the instructions on your screen. The app password is the 16-character code that generates on your device. Select Done.

3) Configure Email Properties

In your application.yml file, configure the email properties for your SMTP server. Replace the placeholders with your actual email server details:

4) Create a Service for Sending Emails

Create a service class that will handle the email sending logic.

The JavaMailSender interface in Spring Framework is a powerful abstraction that provides a convenient way to send email messages using the JavaMail API. It encapsulates the necessary configuration and functionality required for sending emails.

5) Use the Email Service

You can now use the EmailService to send emails in your application. Inject the service into the desired component and call the sendEmail method.

6) Test

You can now test your API using a tool like cURL, Postman, or /swagger-ui. For example, you can use swagger-ui to make requests from the browser:

Source Code

Index
How to Implement PostgreSQL Full-Text Search with Spring Boot

15 min

Spring's transaction management with the @Transactional annotation

9 min

Spring Boot Rest APIs with PostgreSQL (Spring Boot + Rest APIs)

15 min

Caching in Spring Boot (@Cacheable, @CacheEvict & @CachePut)

21 min

Declarative REST Client in Spring Boot (Spring 6 HTTP Interface)

13 min

Circuit Breaker in Spring Boot (Spring Cloud Circuit Breaker + Resilience4j)

12 min

Handling Concurrent Service Calls in a Spring Boot Application: Asynchronous vs. Reactive Approaches

11 min

Profiling a Spring Boot application with Pyroscope

7 min

Service discovery in Spring Boot (Spring Cloud + Netflix Eureka)

9 min

Dockerize Spring Boot app and Push image to DockerHub (Spring Boot + DockerHub)

4 min

Creating a Jenkins Pipeline for Spring Boot application

2 min

Monitoring Microservices (Spring Boot + Micrometer + Prometheus + Grafana)

7 min

Edge Server Pattern in Microservices (Spring Cloud Gateway)

7 min

Circuit Breaker Pattern in Microservices (Spring BOOT + Resilience4j)

4 min

Spring Cloud config server setup with Git

8 min

Distributed Tracing in Microservices (Spring Cloud Sleuth + Zipkin)

9 min

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

24 min

Deploying Spring Boot microservices on Kubernetes Cluster

12 min

Reactive programming in Java with Project Reactor

50 min

Spring Reactive with PostgreSQL (Spring Boot WebFlux + PostgreSQL)

13 min

Spring Reactive, Thymeleaf Hello World (Spring Webflux + Thymeleaf + JS/CSS)

9 min

Problem JSON (application/problem+json) in Spring WebFlux

15 min

Spring Boot Login/Logout (Spring Security + MySql + Thymeleaf)

21 min

Securing Server-to-Server Communication with "Spring Boot" & "OAuth 2"

18 min

Integrating Elasticsearch with a Spring Boot and PostgreSQL application

16 min

Sending Emails in Spring Boot via SMTP

7 min

How to create a basic Spring 6 project using Maven

5 min

Spring Boot, Thymeleaf Hello World (Spring Boot + Thymeleaf + JS/CSS)

9 min