Thursday 13 June 2013

SMS using asp.net


Introduction- Hello "Developers"!! How are you friends? Through this article, i will show how , we can send Sms by  using any gatway url. This article will very helpful for all the .net developers to send sms..

Implementation- create a website , add page named negative_chart.aspx. place a literal control at the design side of this page. Below I am giving the complete code for .aspx page and also .aspx.cs page.



Code for SendSms.aspx Page-

<head runat="server">
    <title>SMS</title>
</head>
<body>
   
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btn_snd_sms" runat="server" Text="Send Sms"
            onclick="btn_snd_sms_Click" />
    </div>
    </form>
</body>
</html>

Code for SendSms.aspx.cs Page-


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class sms : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // Here i am creatind a string function to send the sms.
    public string SendSms(string mobile_number, string message)
    {


        try
        { // i am taking a string to cath the complete url
            string baseurl = null;
            // here i am using the dummy url, this url will not working.
            // Whwn you will buy sms from any sms gatway then they will provide any url. on that url you will pass the two parameters
            //like as mobile number and you message which you want to send.
          // In the below url i am passing two parameters first one is mobile number and second one is message.
            baseurl = "http://168.10.45.88:9345/BulkMessaging/sendingSMS?ani=" + mobile_number + "&uname=FACEBOOK&passwd=batala852&cli=FACEBOOK&message=" + message;
            // System.Net.WebClient  Provides common methods for sending data to and receiving data from a resource identified by a URI.
            System.Net.WebClient client = new System.Net.WebClient();
            //IOstreams can be used for a wide variety of data manipulations
            // in System.IO.Stream data i am passing the complete url with mobile number and message
            System.IO.Stream data = client.OpenRead(baseurl);
            //The StreamReader class is designed for character input in a particular Encoding, whereas subclasses of Stream are designed for byte input and output.
            System.IO.StreamReader reader = new System.IO.StreamReader(data);
            // When complete sms will be sent to mentioned number then a successfully message will come in the string s variable
            string s = reader.ReadToEnd();
            data.Close();
            reader.Close();
            return s;
        }
        catch (Exception ex)
        {
            // if there will be any error then that error will come inside this exception.
            throw ex;

        }

    }

    protected void btn_snd_sms_Click(object sender, EventArgs e)
    {
        // in the number string variable i am passing mobile number and in the message_client variable i am passing the message.
        string number,message_client;
        number = "9646073316";
        message_client = "http://usingaspdotnet.blogspot.in/";
        // Here i am calling the method and passing the parameters
        SendSms(number, message_client);
    }
}

Conclusion- Through this article, you have learned how we can send SMS by using any url or any gatway in Asp.net. If you have any idea to improve this article, please send  on my mail id bharti22200@yahoo.com, I will try to improve my next article.

Regards
Using Asp.net