Tuesday 30 October 2012

Sending SMS using Asp.net C#

To implement this concept you need to follow the below steps :

Step1 :
Create a new Asp.net website in Visual Studio and write the following html code in the design part of the Default.aspx page.

Default.aspx : 
<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 38%;
            height: 163px;
            background-color: #99CCFF;
        }
        .auto-style3 {
            color: #3366FF;
            text-align: center;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>   
        <h2 class="auto-style3"><strong><em>Sending SMS</em></strong></h2>
&nbsp;</div>
        <div> 
            <table align="center" class="auto-style1">
                <tr>
                    <td>Recipient Number :</td>
                    <td>
                        <asp:TextBox ID="txtRecipient" runat="server" Height="16px" Width="199px"></asp:TextBox>
                    </td>
                </tr><tr>
                    <td>Message Body :</td>
                    <td>
                        <asp:TextBox ID="txtMessageBody" runat="server" Height="60px"
                            TextMode="MultiLine" Width="201px"></asp:TextBox>
                    </td>
                </tr><tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Button ID="btnSendSms" runat="server" OnClick="btnSendSms_Click"
                            Text="Send" />
                    </td></tr>
                <tr>
                    <td>&nbsp;</td>
                    <td>
                        <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
                    </td></tr>
            </table>
        </div>
    </form>
</body>
</html>

Step2:

Now open the Default.aspx.cs page and write the following source code.

Default.aspx.cs Code : 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

    }
    protected void btnSendSms_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            string yourmobilenumber = "9985736900";
            string yourpassword = "xxxxxx";
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://ubaid.tk/sms/sms.aspx?uid=" + yourmobilenumber + "&pwd=" + yourpassword + "&msg=" + txtMessageBody.Text + "&phone=" + txtRecipient.Text + "&provider=way2sms");
            HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
            System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
            string responseString = respStreamReader.ReadToEnd();
            respStreamReader.Close();
            Label1.Text = "Sms Sent Successfully";
            myResp.Close();
        }
    }
}

Step3:

Now build the Solution and Debug it for the output.
OutPut :