Banner Ad

Tuesday, August 18, 2015

How to Send SMS From ASP.Net Web Application?

By Francis   Posted at   7:14 PM   SMS Gateways 20 comments
Disclaimer:
            In this article I have used a Third party SMS Gateway service provider, which is I have personally used and experienced.
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’s 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!
<html xmlns="http://www.w3.org/1999/xhtml">    
   <head runat="server">    
      <title></title>    
   </head>    
   <body>    
      <form id="form1" runat="server">    
         <div>    
            <asp:Label runat="server" Text="Enter Mobile Number"></asp:Label>    
            <asp:TextBox runat="server"></asp:TextBox>    
            <br />    
            <asp:Button runat="server" ID="btnSent" Text="Sent SMS" OnClick="btnSent_Click"></asp:Button>    
         </div>    
      </form>    
   </body>    
</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.

protected void btnSent_Click(object sender, EventArgs e)  
{  
     // use the API URL here  
     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";  
     // Create a request object  
     WebRequest request = HttpWebRequest.Create(strUrl);  
     // Get the response back  
     HttpWebResponse response = (HttpWebResponse)request.GetResponse();  
     Stream s = (Stream)response.GetResponseStream();  
     StreamReader readStream = new StreamReader(s);  
     string dataString = readStream.ReadToEnd();  
     response.Close();  
     s.Close();  
     readStream.Close();  
}
That’s all! You have successfully created an application to send SMS messages.
Happy coding!

About Francis

Francis, an Associate at Cognizant. Having 7+ Years of experience in Microsoft web technologies.

20 comments :

  1. Replies
    1. Yes. If you copy this entire code it will not working. Have you substitute your username and password ? What error you got? Let me know i will help you.

      Delete
  2. Code Is Execute but message is not Send

    ReplyDelete
    Replies
    1. As i stated above, Yes. If you copy this entire code it will not working. Have you substitute your username and password?

      Delete
  3. DND numbers will not receive messages. check with other numbers. hope this will help you.

    ReplyDelete
  4. Replies
    1. "Sender ID" is a unique ID, which is provided by the SMS Service Provider. It may be a number or 6 or 8 character string, which will be displayed at the header of the message. Hope this helps!

      Delete
  5. From where do we get this session id. How to get it? Please tell me steps

    ReplyDelete
    Replies
    1. I'm not using any Session Id in my code. What you meant Exactly?

      Delete
  6. messg didnt received at receivers end. Can u please send me code on singhankur0501@gmail.com

    ReplyDelete
    Replies
    1. I would suggest you go with my other article, it will help you to understand the SMS Gateway services more clearly:
      http://www.aspdotnetchamp.com/2013/11/learn-by-experience-sms-gateways.html

      If still you have doubts, contact me i will help you.

      Delete
  7. though I entered uname and password in url unable tos end sms.

    ReplyDelete
    Replies
    1. Have you got any specific error message? What type of SMS your are using either Promotional or Transactional? If you choose promotional than check your number is not listed on DND (Do Not Disturb) list. Also, promotional route sms will take some time to reach. Hope this helps!

      Delete
  8. I used that code . But it is not working properly. please send other . web design company in india

    ReplyDelete
  9. can you please send me other example of this type of application

    ReplyDelete
  10. could you please Explain or show one more example with coding

    ReplyDelete
    Replies
    1. Yeah Sure! Could you point out in which place you are struggling?

      Delete
  11. Which Namespace to use in C#. It's showing namespace error and I am not able to find it

    ReplyDelete
    Replies
    1. Aakash, Sorry for the delayed response! :) I guess, i',m not including any specific namespaces for this. Have you got any specific error, if so let me know i will try to resolve your problem! Happy coding!

      Delete
  12. I got it to send an SMS using bulksms.com example url. But, from your eaxample, I do not see how your form is passing the mobile number to the stringurl. So all I see your example doing is a button to go to a link. There is also no place for the response to be displayed!

    ReplyDelete

Please give your valuable comments to improve the contents

Connect with Us