Skip to main content

Sending Emails in Spring Boot via SMTP

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


Comments

Popular posts from this blog

Deploying Spring Boot microservices on Kubernetes Cluster

This article guides you through the deployment of two Spring Boot microservices, namely "order-service" and "inventory-service," on Kubernetes using "MiniKube" . We will establish communication between them, with "order-service" making calls to an endpoint in "inventory-service." Additionally, we will configure "order-service" to be accessible from the local machine's browser . 1) Create Spring Boot microservices The Spring Boot microservices, "order-service" and "inventory-service," have been developed and can be found in this GitHub repository. If you are interested in learning more about creating Spring Boot REST microservices, please refer to this or this (Reactive) link. 2) Build Docker Images The Docker images for both "order-service" and "inventory-service" have already been generated and deployed on DockerHub, as shown below. codeburps/order-service cod...

Circuit Breaker Pattern with Resilience4J in a Spring Boot Application

Read Also: Spring Cloud Circuit Breaker + Resilience4j Resilience4j is a lightweight fault tolerance library that draws inspiration from Netflix Hystrix but is specifically crafted for functional programming. The library offers higher-order functions, known as decorators , designed to augment any functional interface, lambda expression, or method reference with features such as Circuit Breaker, Rate Limiter, Retry, or Bulkhead . These functionalities can be seamlessly integrated within a project, class, or even applied to a single method. It's possible to layer multiple decorators on any functional interface, lambda expression, or method reference, allowing for versatile and customizable fault tolerance. While numerous annotation-based implementations exist online, this article focuses solely on the reactive approach using router predicates and router functions . How Circuit Breaker Pattern works? In general, a circuit breaker functions as an automatic electrical s...

How to create a basic Spring 6 project using Maven

Below is a step-by-step guide to creating a basic Spring project using Maven. 1) Create a Maven Project Use the following Maven command to create a new Maven project. mvn archetype:generate -DgroupId=com.tb -DartifactId=spring-demo -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 2) Import in IntelliJ IDEA If you haven't already, open IntelliJ IDEA on your system. Go to "File" > "New" > "Project from Existing Sources..." . In the file dialog, navigate to the directory where your Maven project is located. Select the pom.xml file within the project directory and click "Open." 3) Update pom.xml In total, the application requires the below-mentioned dependencies: 4) Create Spring Configuration Create a Java configuration class that uses annotations to define your Spring beans and their dependencies. This class should be annotated with @Configuration . 5) Create the Main Application C...