This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

24 Dec 2013

Sending SMS using HTTP API in C#.Net / VB.Net Winforms Application

In this article we will see an example of sending SMS from our winforms C#.Net or VB.Net desktop Application using HTTP API  and tracking the status of SMS .
Below is the screen shot of Application



In this example i have used the HTTP API of www.smsonline.co.in .
In order to send SMS, you should have an account with the Service Provider. The HTTP API differs for different service providers.
When you buy SMS credits, you will get UserName and Password, which you have to replace in  HTTP API provided by respective service provider.

The concept here is, we have to send HTTP request from our code. It can be done in many ways.  The same code you can also use in your Asp.Net Application. You can download the Application full Source Code from the link provided below.

The C# Code to send SMS is like this

         /// <summary>
        /// Function send Http request and returns the status of the message
        /// </summary>
        /// <param name="strMsg">Message to be sent</param>
        /// <param name="MobNo">MobileNumber</param>
        /// <returns></returns>
        private string SendSMS(string strMsg, string MobNo)
        {
            string SmsStatusMsg = string.Empty;
            try
            {
                //Sending SMS To User
                WebClient client = new WebClient();
                string URL = "http://smsonline.co.in/sendsmsv2.asp?user=UserName&password=Password&sender=Internet&text=" + strMsg + "&PhoneNumber=" + MobNo + "&track=1";

                SmsStatusMsg = client.DownloadString(URL);
                if (SmsStatusMsg.Contains("<br>"))
                {
                    SmsStatusMsg = SmsStatusMsg.Replace("<br>", ", ");
                }

            }
            catch (WebException e1)
            {
                SmsStatusMsg = e1.Message;
            }
            catch (Exception e2)
            {
                SmsStatusMsg = e2.Message;
            }
            return SmsStatusMsg;

        }


The VB.Net Code for sending SMS is :

    ''' <summary>
    ''' Function send Http request and returns the status of the message
    ''' </summary>
    ''' <param name="strMsg">Message to be sent</param>
    ''' <param name="MobNo">mobile Number</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SendSMS(ByVal strMsg As String, ByVal MobNo As String) As String
        Dim SmsStatusMsg As String = String.Empty
        Try
            'Sending SMS To User
            Dim client As WebClient = New WebClient()
            Dim URL As String = "http://smsonline.co.in/sendsmsv2.asp?user=UserName&password=Password&sender=Internet&text=" & strMsg & "&PhoneNumber="& MobNo &"&track=1"

            SmsStatusMsg = client.DownloadString(URL)
            If SmsStatusMsg.Contains("<br>") Then
                SmsStatusMsg = SmsStatusMsg.Replace("<br>", ", ")
            End If

        Catch e1 As WebException
            SmsStatusMsg = e1.Message
        Catch e2 As Exception
            SmsStatusMsg = e2.Message
        End Try
        Return SmsStatusMsg

    End Function

Download Source Code