0

i have a requirement of sending sms to different phone number from asp.net and I have absolutely no idea of sending sms. But through google i have got this piece of code :

   using System;
   using System.Collections.Generic;
   using System.Configuration;
   using System.Data;
   using System.Linq;
   using System.Web;
   using System.Web.Security;
   using System.Web.UI;
   using System.Web.UI.HtmlControls;
   using System.Web.UI.WebControls;
   using System.Web.UI.WebControls.WebParts;
   using System.Xml.Linq;
   using System.Net;


 namespace TESTING
  {
public partial class SendSMS : System.Web.UI.Page
{
    string uid;
    string password;
    string message;
    string no;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void send()
    {
    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://ubaid.tk/sms/sms.aspx?uid=" + uid + "&pwd=" + password + "&msg=" + message + "&phone=" + no + "&provider=fullonsms");
    HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
    System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
    string responseString = respStreamReader.ReadToEnd();
    respStreamReader.Close();
    myResp.Close();
}

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        uid = "";
        password = "";
        message = MessageTextBox.Text;
        no = MobileNumberTextBox.Text;
        send();
        MessageTextBox.Text = "";
        MobileNumberTextBox.Text = "";
    }
    catch (Exception ex)
    {
        ex.Message.ToString();            
    }

}

 }
 }

I have a asp code like this:

  <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendSMS.aspx.cs" Inherits="TESTING.SendSMS" %>

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
     <title></title>
    </head>
  <body>
   <form id="form1" runat="server">
    <div>
      <asp:Label ID="Label1" runat="server" Text="Mobile No"></asp:Label>
       <asp:TextBox ID="MobileNumberTextBox" runat="server"></asp:TextBox>
        <br />
          <asp:Label ID="Label2" runat="server" Text="Message"></asp:Label>
            &nbsp;&nbsp;
            <asp:TextBox ID="MessageTextBox" runat="server" TextMode="MultiLine">                     </asp:TextBox>
        <br />
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Send Sms" OnClick="Button1_Click" />
      </div>
     </form>
   </body>

this piece of code is not wroking for sending sms. I need a code sample to send the sms. Your help means a lot to me.

Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
Arunesh
  • 287
  • 6
  • 18
  • Search the web for "sms provider". – CodeCaster Oct 04 '13 at 05:59
  • 1
    You need to purchase SMS plan from one of the hosting service provider. They will give you the username and password as well as required code block. Just place that code block in your aplication code block. – siddhesh Oct 04 '13 at 06:09

2 Answers2

1

You were not passing text box values of message , password , no , uid to send() function. aspx file seems fine. do changes in cs file like below.

For sake of less code ,I have replaced HttpWebRequest class with WebClient class for downloading url .
WebClient class provides for most of functionality provided by HttpWebRequest class.

For comparison between the two , see this discussion thread.

   using System;
   using System.Collections.Generic;
   using System.Configuration;
   using System.Data;
   using System.Linq;
   using System.Web;
   using System.Web.Security;
   using System.Web.UI;
   using System.Web.UI.HtmlControls;
   using System.Web.UI.WebControls;
   using System.Web.UI.WebControls.WebParts;
   using System.Xml.Linq;
   using System.Net;


 namespace TESTING
{
public partial class SendSMS : System.Web.UI.Page
{
    string uid;
    string password;
    string message;
    string no;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void send(string message ,string no,string password,string uid )
    {

     using (WebClient cli = new WebClient())
    {
      url =@"http://ubaid.tk/sms/sms.aspx?uid=" + uid + "&pwd=" + password + "&msg=" + message + "&phone=" + no + "&provider=fullonsms";
      cli.DownloadString(url);

    }

    }

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        uid = "";
        password = "";
        message = MessageTextBox.Text;
        no = MobileNumberTextBox.Text;
        send(message ,no,password,uid );
        MessageTextBox.Text = "";
        MobileNumberTextBox.Text = "";
    }
    catch (Exception ex)
    {
        ex.Message.ToString();            
    }

}

 }
 }

I have used WebClient class present in System.Net namespace which downloads the web address specified in string url by its DownloadString() function .

See here for WebClient class

Community
  • 1
  • 1
Mudassir Hasan
  • 28,083
  • 20
  • 99
  • 133
0

You need to get a better SMS provider. I’ve checked Ubaid you are using in the example above and it looks like

a) you are not providing user and password and

b) this service is not really reliable.

I’ve looked at the comments on their website and many people say it just doesn’t work.

Try searching google for SMS provider API.

  • hi mmhasannn. I used Your application which you have posted as above.but it getting exception as "The remote server returned an error: (503) Server Unavailable". – kavitha Reddy Aug 04 '14 at 10:23