Introduction
In this blog, you will learn how to send OTP SMS on user mobile number using ASP.NET C#
Step 1
Create a new website.
Step 2
Create a webform aspx page.
Step 3
Design the webpage like below,
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <table style="border:solid;border-width:thin"> <tr> <td><asp:Label ID="lblmobele" runat="server" Text="Enter Mobile Number"></asp:Label></td> <td><asp:TextBox ID="txtmobileno" runat="server"></asp:TextBox></td> </tr> <tr> <td></td> <td><asp:Button ID="btnSendOTP" runat="server" Text="Send OTP" OnClick="btnSendOTP_Click"></asp:Button></td> </tr> </table> <asp:Label ID="lblMsg" runat="server" Text=""></asp:Label> </div> </form> </body> </html>
Step 4
Added below namespace in .aspx.cs page,
using System.Drawing; using System.Web.UI.WebControls; using System.Data; using System.Web; using System.Net; using System.IO; using System.Web.UI; using System.Collections.Generic; using System.Linq;
Step 5
Add the below source code in .aspx.cs page.
// Start OTP Generation function protected string Generate_otp() { char[] charArr = "0123456789".ToCharArray(); string strrandom = string.Empty; Random objran = new Random(); for (int i = 0; i < 4; i++) { //It will not allow Repetation of Characters int pos = objran.Next(1, charArr.Length); if (!strrandom.Contains(charArr.GetValue(pos).ToString())) strrandom += charArr.GetValue(pos); else i--; } return strrandom; } // End OTP Generation function // Start Send OTP code on button click protected void btnSendOTP_Click(object sender, EventArgs e) { string otp = Generate_otp(); string mobileNo = txtmobileno.Text.Trim(); string SMSContents = "", smsResult = ""; SMSContents = otp + " is your One-Time Password, valid for 10 minutes only, Please do not share your OTP with anyone."; smsResult = SendSMS(mobileNo, SMSContents); txtmobileno.Focus(); lblMsg.Text = " OTP is sent to your registered mobile no."; lblMsg.CssClass = "green"; mobileNo = string.Empty; txtmobileno.Focus(); Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "countdown()", true); } // End Send OTP code on button click // SMS Sending function public static string SendSMS(string MblNo, string Msg) { string MainUrl = "SMSAPIURL"; //Here need to give SMS API URL string UserName = "username"; //Here need to give username string Password = "Password"; //Here need to give Password string SenderId = "SenderId"; string strMobileno = MblNo; string URL = ""; URL = MainUrl + "username=" + UserName + "&msg_token=" + Password + "&sender_id=" + SenderId + "&message=" + HttpUtility.UrlEncode(Msg).Trim() + "&mobile=" + strMobileno.Trim() + ""; string strResponce = GetResponse(URL); string msg = ""; if (strResponce.Equals("Fail")) { msg = "Fail"; } else { msg = strResponce; } return msg; } // End SMS Sending function // Get Response function public static string GetResponse(string smsURL) { try { WebClient objWebClient = new WebClient(); System.IO.StreamReader reader = new System.IO.StreamReader(objWebClient.OpenRead(smsURL)); string ResultHTML = reader.ReadToEnd(); return ResultHTML; } catch (Exception) { return "Fail"; } } // End Get Response function