Send OTP via SMS and Email using Azure Communication Services with ASP.NET Core | Step-by-Step Guide
- Set up Azure Communication Services for SMS and Email.
- Build an ASP.NET Core API to handle OTP requests.
- Implement OTP generation and delivery through SMS and Email.
By the end of this guide, you'll have a fully functional solution for sending OTPs via SMS and Email using Azure's powerful communication platform.
- Set up Azure Communication Services for SMS and Email.
- Build an ASP.NET Core API to handle OTP requests.
- Implement OTP generation and delivery through SMS and Email.
1. Overview
This guide will walk you through the process of setting up Azure Communication Services (ACS), integrating it with ASP.NET Core, and sending OTPs via both SMS and Email.
✅ Prerequisites
- Azure Subscription
- Azure Communication Services (ACS) Resource
- Phone Number Purchased in ACS (For SMS)
- Email Feature Enabled in ACS (For Email OTPs)
- ASP.NET Core Web API
- .NET 6 or later
2. Setting Up Azure Communication Services (ACS)
Step 1: Create ACS Resource
1️⃣ Log in to the Azure Portal.
2️⃣ Search for "Azure Communication Services" and create a new resource.
3️⃣ Copy the Connection String from the Keys section.
4️⃣ Purchase a Phone Number under Phone Numbers (Required for SMS).
Step 2: Enable Email Feature in ACS
1️⃣ Navigate to Azure Communication Services → Email.
2️⃣ Click Set Up Email → Follow the steps to link an email domain.
3️⃣ Obtain the Email Connection String under the Keys section.
3. Setting Up an ASP.NET Core Project
Step 1: Create a New ASP.NET Core Web API Project
Run the following command in the terminal:
Step 2: Install Required NuGet Packages
Run the following command to install Azure SDKs:
4. Implementing OTP Service in ASP.NET Core
Step 1: Configure ACS in appsettings.json
Step 2: Create OtpService.cs
Step 3: Create OtpController.cs
Step 4: Register OtpService in Program.cs
Modify Program.cs to inject OtpService:
5. Testing the API using Postman
Test OTP via SMS
- Endpoint:
POST http://localhost:5000/api/otp/send-sms - Body (JSON):
Test OTP via Email
- Endpoint:
POST http://localhost:5000/api/otp/send-email - Body (JSON):
6. Running the Application
Start the ASP.NET Core API:
You should see logs confirming OTPs sent via SMS and Email.

Comments
Post a Comment