如何通过Asp.net Web应用程序发送SMS

时间:2012-01-29 07:20:29

标签: asp.net multithreading sms

如何从asp.net Web应用程序发送短信?

是否有必要从任何提供商处购买?

是否可以发送短信数量限制?

是否应该在不同的线程中发送SMS代码,以便在出现任何异常/延迟时主线程不会受到影响?

有人可以提供代码示例以及它如何工作的概念吗?

3 个答案:

答案 0 :(得分:1)

您需要利用某种电话提供商来实现这一目标。我个人是Twilio的忠实粉丝。它们是非常合理的费率,并API展示language specific API wrappers。特别是.NET有几个,包括官方的。

可以使用代码示例和演练here

性能影响很小,因为您实际上构造了一个传递给Twilio端点的XML消息。繁重的工作不再是你的肩膀,因为它更像是传递和接收XML信息的系统。

希望有所帮助,这是一个非常容易使用的平台。

答案 1 :(得分:1)

这里只是配置你的在线短信提供商* /这里“way2sms is configure”

using System; 
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;

namespace SMSAPI
{
class SmsSender
{
void send(string uid, string pwd, string no, string msg)
{
String content = "username="+uid+"&password="+pwd;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/auth.cl");
request.KeepAlive = false;
byte[] byteArray = Encoding.UTF8.GetBytes(content);
CookieContainer cookies = new CookieContainer();
request.CookieContainer = cookies;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.Referer = "http://wwwg.way2sms.com//entry.jsp";
request.Method = "POST";
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
foreach (Cookie cook in response.Cookies)
{
cookies.Add(cook);
}
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream);
string serverData = reader.ReadToEnd();
reader.Close();
content = "custid=undefined&HiddenAction=instantsms&Action=custfrom450000&login=&pass=&MobNo="+no+"&textArea="+msg;
request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/FirstServletsms?custid=");
byteArray = Encoding.UTF8.GetBytes(content);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.CookieContainer = cookies;
request.Method = "POST";
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
reader = new StreamReader(stream);
serverData = reader.ReadToEnd();
reader.Close();
request = (HttpWebRequest)WebRequest.Create("http://wwwa.way2sms.com/jsp/logout.jsp");
byteArray = Encoding.UTF8.GetBytes(content);
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5";
request.ContentLength = byteArray.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.Accept = "*/*";
request.CookieContainer = cookies;
request.Method = "POST";
dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = (HttpWebResponse)request.GetResponse();
stream = response.GetResponseStream();
reader = new StreamReader(stream);
serverData = reader.ReadToEnd();
reader.Close();
}
catch (ArgumentException e)
{
Console.WriteLine("arg exception");
Console.Read();

}
catch (WebException e)
{
Console.WriteLine("web exception");
Console.Read();
}
catch (Exception e)
{
Console.WriteLine("exception");
Console.Read();
}   
}

static void Main(string[] args)
{
    SmsSender sms = new SmsSender();
sms.send("username", "password", "phno_recipient", "message");
}

}
}

答案 2 :(得分:0)

我使用了TcpClient,它实现了Disposable&创建了SmsHelper     类

public class SmsHelper
{
    public void SendSms(string toNumber, string content)
    {
        bool connected;

        TcpClient smsServer = OpenConnection("xyz.xy.x.xyz", xyzd, out connected);//require ip and port

        if (connected)
        {
            string sms = content; 

            SendSmsToClient(smsServer, Properties.Settings.Default.FromNumber, toNumber, sms);

        }
    }

    protected static TcpClient OpenConnection(string ip, int port, out bool connected)
    {
        string response = string.Empty;
        string message = string.Empty;

        TcpClient tcpClient = new TcpClient();

        try
        {
            ASCIIEncoding ascEn = new ASCIIEncoding();

            tcpClient.Connect(ip, port);

            Stream stream = tcpClient.GetStream();

            byte[] bb = new byte[100];
            stream.Read(bb, 0, 100);

            string connect = ascEn.GetString(bb);

            if (!String.IsNullOrEmpty(connect))
            {
                //authenticating to smsServer
                string str = "action: login\r\nusername: xxxxx\r\nsecret: integration\r\n\r\n";

                byte[] ba = ascEn.GetBytes(str);
                stream.Write(ba, 0, ba.Length);
                stream.Flush();

                byte[] resp = new byte[100];
                stream.Read(resp, 0, 100);
                response = ascEn.GetString(resp);
                stream.Read(resp, 0, 100);
                message = ascEn.GetString(resp);

                if (response.Contains("Success") && message.Contains("Authentication accepted"))
                {
                    Console.WriteLine("Authenticated");
                    stream.Flush();
                    connected = true;
                    return tcpClient;
                }
                else
                {
                    Console.WriteLine("Credentials error Cant Authenticate");
                    tcpClient.Close();
                    connected = false;
                    return tcpClient;
                }
            }

            connected = false;
            return tcpClient;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        connected = false;
        return tcpClient;
    }

    protected static void CloseConnection(TcpClient client)
    {
        client.Close();
        Console.WriteLine("Connection Closed process terminated...");
    }


    protected static void SendSmsToClient(TcpClient client, string fromNumber, string toNumber, string smsBody)
    {
        string response = string.Empty;
        string message = string.Empty;
        string eventMsg = string.Empty;

        ASCIIEncoding asen = new ASCIIEncoding();
        Stream stm = client.GetStream();

        string smsSend = string.Format("action: smscommand\r\ncommand: gsm send sms {0} {1} \r\n\r\n", fromNumber, toNumber);

        byte[] smsCmd = asen.GetBytes(smsSend);

        stm.Write(smsCmd, 0, smsCmd.Length);
        stm.Flush();

        byte[] smsResp = new byte[1000];
        stm.Read(smsResp, 0, 1000);
        response = asen.GetString(smsResp);

        if (!String.IsNullOrEmpty(response))
        {
            stm.Read(smsResp, 0, 1000);
            message = asen.GetString(smsResp);

            if (!String.IsNullOrEmpty(message))
            {
                stm.Read(smsResp, 0, 1000);

                eventMsg = asen.GetString(smsResp);

                if (!String.IsNullOrEmpty(eventMsg))
                {
                    String[] list = eventMsg.Split('\n');

                    foreach (string value in list)
                    {
                        if (value.StartsWith("--END"))
                        {
                            stm.Flush();
                        }
                    }
                }
            }
        }
    }
}