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 : 

11 comments:

  1. hey cn u mail me d proper code at steffi.aiesec@gmail.com

    ReplyDelete
  2. d code is perfect.. but i am not receiving any msg.

    ReplyDelete
    Replies
    1. please check your credentials. It should match your Way2sms account details.

      Delete
  3. code is perfect.. but i am not receiving any msg in mobile

    ReplyDelete
  4. now a day's (http://ubaid.tk/sms/sms.aspx) not working properly so don't go with this code.

    ReplyDelete
  5. then what should i use for sending sms?.

    ReplyDelete
  6. its not working.... still im given my login credentials of way2 sms

    ReplyDelete
  7. Hey this thing is not working just remove it do not waster our time

    ReplyDelete
  8. if url is changed inthe above code will it work

    ReplyDelete