Send OTP via SMS and Email using Azure Communication Services with Spring Boot
This guide will walk you through the entire process of setting up Azure Communication Services, integrating it with Spring Boot, and sending OTPs via both SMS and email.
1. Setting Up Azure Communication Services (ACS)
Step 1: Create an Azure Communication Services Resource
- Go to the Azure Portal → Search for Azure Communication Services → Click Create.
- Provide a name, subscription, resource group, and region.
- Click Review + Create, then Create.
- After the resource is created, go to the Keys section and copy the Connection String.
- Under Phone Numbers, purchase a phone number for sending SMS.
Step 2: Set Up Email in ACS
- Under Azure Communication Services, configure the Email feature in the portal by following the setup steps.
2. Set Up Spring Boot Project
Step 1: Create a Spring Boot Project
You can create a Spring Boot project using Spring Initializr (https://start.spring.io/):
- Project: Maven Project
- Language: Java
- Spring Boot Version: 3.x.x or higher
- Dependencies: Spring Web, Spring Boot DevTools
Alternatively, you can generate the project from the command line:
Step 2: Add Azure Communication Services Dependencies to pom.xml
Step 3: Add Application Properties
In src/main/resources/application.properties, add your Azure Communication Services connection details:
3. Implementing the Service to Send OTPs
Step 1: Create the Azure Communication Service Class
Create a service class that uses ACS SDK to send SMS and Email.
Step 2: Create the OTP Controller
Create a controller to handle the HTTP request and trigger OTP sending via both SMS and Email.
4. Testing the Application
Step 1: Run Your Spring Boot Application
Start your Spring Boot application by running:
Step 2: Test the OTP API
You can use Postman or cURL to test the endpoint:
- URL:
http://localhost:8080/api/otp/send - Method:
POST - Parameters:
phoneNumber: Phone number to send SMS OTP to.email: Email address to send email OTP to.
Example Request (Postman):
5. Summary of the Flow
- Azure Communication Services Setup: Create the ACS resource for both SMS and Email.
- Spring Boot Project Setup: Add the necessary ACS dependencies and configure them.
- OTP Service: The service generates and sends OTP via both SMS and email.
- Controller: Handles the endpoint to receive OTP requests and sends OTP to both mediums.
This complete solution will allow your Spring Boot application to send OTPs via both SMS and email using Azure Communication Services.

Comments
Post a Comment