How to Generate and Send OTP SMS For Free Using C#
how to generate and send otp sms for free using c#

As you all guyz know that security of user account is the critical problem in these days. So, the developer have to think about how they can secure their user account. This is can done by generating and sending OPT to the user phone number through SMS.

First understand what is OTP?

A One Time Password (OTP) is a unique code/token generated by our application, sent to a user via SMS text and then entered into our application login/signup flow for additional security.

  • People supply a mobile number when they join your service.
  • You generate a unique code and merge it into an SMS text and send to the user.
  • The user types the code into a form on your server and you verify the code matches.
  • You delete the code after a successful login or after a period of time.

One-Time Passwords are only effective for a fixed period of time and become invalid once the user logs in, making them exceptionally useful against spyware such as key logging programs.

One you have built One Time Passwords into your activation/signup flow a One Time Password will be texted every time a login attempt is made.  This flow will dramatically improve account security.

“This tutorial may be very useful for those who want to integrate OTP features in their project which is developed in C#.NET”.

So for this, we have to take the help of SMS gateway to send the OTP to the user Phone Number.

We are going to take help of txtlocal as our SMS gateway. All you have do is go to http://www.txtlocal.co.uk and there you need to create one account. after creating account. (you need to fill up some of your information and txtlocal will send you a verification email into your email and verify your account [watch the video for detail]).After completing this step, go http://api.txtlocal.com/docs/sendsms and from the select language option choose C# and then copy the code and edit as in the following manner or simply copy the following code but you need to provide your own api key when you run this project.

Now write the following piece of code after designing the following form in Visual Studio.

STEPS:

1. I am assuming that you have Visual Studio, Open your Visual Studio and click on File>new project, From the left side of your window, select Visual C#>Window and from the middle side select Windows Forms Application and then give the name of your project and then click OK which is shown in below.

2. From the ToolBox Drag and Drop three label, three Textbox and two button in your form and change the properties of these controls by selecting each control and right click that particular control and click on properties.(You can change the color of each control, increase the font of each control)

Three Label for phone no, name, verify OTP
Three Textbox for phone no, name , OTP
Two Button to send OTP and Verify OTP

How to generate and send OTP SMS for free using C#
How to generate and send OTP SMS for free using C#

3. Double click on the Login button to go to the .cs section of your project and scroll up and then add the following namespace to your program as:

using System;
using System.Collections.Generic;
using System.Net;
using System.Collections.Specialized;
using System.IO;

4. Define one global variable inside the class:

string randomNumber

5. Write the following piece of code inside the clicking event of Login Button:


String result;
String name = txtName.Text;
string apiKey = "your_api_key";
string numbers = txtPhone.Text;
Random rnd = new Random();
randomNumber = (rnd.Next(100000, 999999)).ToString();

string message = "Hey "+name+ "your otp is "+randomNumber;
string send = "your_sender_name";

String url = "https://api.txtlocal.com/send/?apikey=" + apiKey + "&numbers=" + numbers + "&message=" + message + "&sender=" + sender;
//refer to parameters to complete correct url string

StreamWriter myWriter = null;
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);

objRequest.Method = "POST";
objRequest.ContentLength = Encoding.UTF8.GetByteCount(url);
objRequest.ContentType = "application/x-www-form-urlencoded";
try
{
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(url);
}
catch (Exception eX)
{
MessageBox.Show(eX.Message);
}
finally
{
myWriter.Close();
}

HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
{
result = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
MessageBox.Show(result);

6. Double click the verify button and write following piece of code:


if (txtVerOTP.Text==randomNumber)
{
MessageBox.Show("logined successfully");
}
else
{

MessageBox.Show("error");
}

7. Now Login to the your txtlocal account, goto setting>API KEY and then click on create API Key and then simply copy that created key.

8. Run your project by clicking in that play button. Enter your Name, Phone number with country code in the respective textbox and then click on Login button. And when you receive OTP enter that OTP in OTP textbox. That’s it.

May be after sending 10 OTP SMS your credit will be finished! but don’t be sad you can send more and more OTP SMS but you need to create another account with another email account in www.txtlocal.co.uk ! that’s it. you may not be that much happy to hear this (sending only 10 OTP SMS) but don’t forget you are able to send these 10 OTP SMS for free and also there is one line “To get something, you must lose something”. it means you have to spend some time in creating account in txtlocal.co.uk in order to send free OTP SMS.

Finally, if you guyz think, this is awesome tutorial and help you in some extend please share this tutorials with your all friend so that they can know about this method of sending OTP SMS!

if you have any confusion, you can watch the following video:

SHARE