Send mobile SMS using website :- I am using ASP.Net C#.It helps a lot for those who develops a web application like E-commerce, etc.
SOURCE CODE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class Phone : System.Web.UI.Page
{
string CS = ConfigurationManager.ConnectionStrings["MyDatabaseConnectionString"].ConnectionString;
DataTable dt = new DataTable();
static int sentOtp = 0;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSend_Click(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(CS))
{
Random rand = new Random();
string apikey = "your api key";
long numbers = Convert.ToInt64(txtPhone.Text);
sentOtp = rand.Next(1000, 9999);
string senders = "TXTLCL";
SqlCommand cmd = new SqlCommand("select * from AdminAccess where phoneNumber ='" + numbers + "'", con);
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count != 0)
{
String url = "https://api.textlocal.in/send/?apikey=" + apikey + "&numbers=" + numbers + "&message=" + sentOtp + "&sender=" + senders;
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);
lblmessage.Text = "OTP Sent Successfully";
lblmessage.ForeColor = System.Drawing.Color.Green;
txtOtp.Visible = true;
btnVerify.Visible = true;
}
catch (Exception)
{
lblmessage.Text = "OTP could not sent";
lblmessage.ForeColor = System.Drawing.Color.Red;
}
finally
{
mywriter.Close();
}
}
else
{
lblmessage.Text = "This Phone Number is not present in our database";
lblmessage.ForeColor = System.Drawing.Color.Red;
}
}
}
protected void btnVerify_Click(object sender, EventArgs e)
{
if (sentOtp == Convert.ToInt32(txtOtp.Text))
{
Session["Result"] = "Successful";
Response.Redirect("~/Admin.aspx");
}
else
{
lblmessage.Text = "OTP does not matches";
lblmessage.ForeColor = System.Drawing.Color.Red;
}
Session["Result"] = "Successful";
Response.Redirect("~/Admin.aspx");
}
}