如何将短信发送到不同的电话号码

时间:2013-10-04 05:31:49

标签: c# asp.net

我要求从asp.net向不同的电话号码发送短信,我完全不知道发送短信。但通过谷歌我得到了这段代码:

   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();            
    }

}

 }
 }

我有一个像这样的asp代码:

  <%@ 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>

这段代码并不是在发送短信。我需要一个代码示例来发送短信。你的帮助对我意义重大。

2 个答案:

答案 0 :(得分:1)

您没有将消息,密码,否,uid的文本框值传递给send()函数。  aspx文件似乎很好。像下面一样对cs文件进行更改。

为了减少代码量,我已将HttpWebRequest类替换为WebClient类以下载URL。
WebClient类提供HttpWebRequest类提供的大多数功能。

两者之间的比较,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();            
    }

}

 }
 }

我在WebClient命名空间中使用了System.Net类,它下载了字符串url中指定的网址 DownloadString()功能。

See here for WebClient class

答案 1 :(得分:0)

您需要获得更好的短信服务提供商。我在上面的示例中检查了您正在使用的Ubaid,它看起来像

a)您没有提供用户和密码

b)这项服务真的不可靠。

我查看了他们网站上的评论,很多人说它不起作用。

尝试在Google上搜索SMS provider API