How to send SMS through Asp.net Web Application

后端 未结 3 1980
失恋的感觉
失恋的感觉 2021-01-29 06:25

How to send SMS from asp.net web application?

Is it necessary to buy from any provider?

Is there any limitation of number of SMS can be sent?

Should send

相关标签:
3条回答
  • 2021-01-29 06:45

    You will need to utilize some sort of telephony provider to achieve this. I personally am a big fan of Twilio. They are very reasonable rates and expose an API with language specific API wrappers. .NET in particular has several, including an official one.

    Code samples and walkthroughs are available here.

    The performance impact is minimal, as you essentially construct an XML message that gets passed to a Twilio endpoint. The heavy lifting is off your shoulders as its more a system of passing and receiving XML messages.

    Hope that helps, it is a very easy platform to work with.

    0 讨论(0)
  • 2021-01-29 06:54

    I used TcpClient, which implements Disposable & created SmsHelper class

    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();
                            }
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-29 07:06

    here just confiure your online sms provider */ here "way2sms is confiure"

    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");
    }
    
    }
    }
    
    0 讨论(0)
提交回复
热议问题