Advertisement

Advertisement

How to send otp on any mobile number using Twilio

How to send otp on any mobile number using Twilio


Step 1. Create .net core 8 console app.

Step 2. install Twilio from  nuget package (just type Twilio in Manage nuget package, 
            +75M downloaded)

or just paste it in edit Project file

 <ItemGroup>
   <PackageReference Include="Twilio" Version="7.8.0" />
 </ItemGroup>

Step 3. Write down the code in main method Program.cs

using System;
using Twilio;
using Twilio.Rest.Api.V2010.Account;

class Program
{
    static void Main(string[] args)
    {
        // Twilio Account SID aur Auth Token (Twilio Console se lein)
        const string accountSid = "AC55d242a0054a1aa9818dac9c49637....";
        const string authToken = "4f21c3dbe6321f1201c39ed9e67e5.....";

        // Twilio Client Initialize
        TwilioClient.Init(accountSid, authToken);

        // OTP Generate karna
        Random random = new Random();
        string otp = random.Next(100000, 999999).ToString();

        // Message bhejna
        var message = MessageResource.Create(
            body: $"Your OTP is {otp}.",
            from: new Twilio.Types.PhoneNumber("+1 570 539 ......."), // Twilio Verified Number
            to: new Twilio.Types.PhoneNumber("+91834026669...")     // User's Phone Number
        );

        Console.WriteLine($"OTP Sent: {otp} | Message SID: {message.Sid}");
        Console.ReadLine();
    }
}




How to send otp on any mobile number using Twilio How to send otp on any mobile number using Twilio Reviewed by Rikesh on January 09, 2025 Rating: 5

No comments:

Powered by Blogger.