How to Send SMS From ASP.Net Web Application

Introduction:

In my previous article I explained SMS Gateways and the various schemes available for them. This article explains how to integrate those SMS gateways into an ASP.Net web application.

What a "SMS Gateway" is

A SMS Gateway allows a computer to send or receive SMS to or from a telecommunications network. This Wikipedia URL provides more detail.

Some “facts” about SMS Gateways

When I did some experimentation and Googling of SMS gateways some of the tutorials suggested use of “free” SMS gateways. But nothing worked for me. Latter I understood that “Nothing comes free”. Also most people asked the same question: “Is there any free SMS service is available to send SMS from my website?”. In my experience the answer is no. So my point here is, don’t waste your time searching for the free API. Instead, find a well-suited SMS Gateway provider and sign up with them.

What next

Well you want to send a SMS from your ASP.Net web application. So as a first step, you need to sign up with a SMS Gateway provider to get the SMS credits. Normally all SMS gateway providers provide at least 10 free SMS credits to check their services. Also they will provide details of the Application Programming Interfaces (APIs) and explain in detail how to integrate those API codes into your application.

For demo purposes I have signed up an account with mvaayoo SMS gateway providers and will use their services in this tutorial.

Markup

For the simple illustrative purposes, I’m going to develop a simple UI in a web-form as in the following. I have a textbox to accept mobile numbers and a button to send the message. That’s all!

  1. <html xmlns="http://www.w3.org/1999/xhtml">    
  2.    <head runat="server">    
  3.       <title></title>    
  4.    </head>    
  5.    <body>    
  6.       <form id="form1" runat="server">    
  7.          <div>    
  8.             <asp:Label runat="server" Text="Enter Mobile Number"></asp:Label>    
  9.             <asp:TextBox runat="server"></asp:TextBox>    
  10.             <br />    
  11.             <asp:Button runat="server" ID="btnSent" Text="Sent SMS" OnClick="btnSent_Click"></asp:Button>    
  12.          </div>    
  13.       </form>    
  14.    </body>    
  15. </html>   

Code behind

In the code behind file, we need to copy and paste the API code provided by the SMS Gateway. As I already explained, every SMS Gateway service provider provides the API code to be used in your application. In mvaayoo, they provide the API code for ASP.Net, ASP, VB and PHP too. You need to substitute the user name password and sender id in the respective places in the URL. You need to also specify the mobile number too in the URL. If you want to send the SMS to multiple numbers then you need to provide those numbers in comma-separated format.

  1. protected void btnSent_Click(object sender, EventArgs e)  
  2. {  
  3.      // use the API URL here  
  4.      string strUrl = "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user=YourUserName:YourPassword&senderID=YourSenderID&    receipientno=1234567890&msgtxt=This is a test from mVaayoo API&state=4";  
  5.      // Create a request object  
  6.      WebRequest request = HttpWebRequest.Create(strUrl);  
  7.      // Get the response back  
  8.      HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
  9.      Stream s = (Stream)response.GetResponseStream();  
  10.      StreamReader readStream = new StreamReader(s);  
  11.      string dataString = readStream.ReadToEnd();  
  12.      response.Close();  
  13.      s.Close();  
  14.      readStream.Close();  

That’s all! You have successfully created an application to send SMS messages.

Happy coding!