簡體   English   中英

如何使用 asp.net 和 c# 從 web 應用程序向手機發送短信?

[英]how to send sms to mobile from web application using asp.net and c#?

如何使用 asp.net 和 c# 從 web 應用程序向手機發送短信?

大多數運營商提供 email 后綴,可用於通過 email 發送短信。 一般來說:

[PhoneNumber]@[Suffix]

您可以聯系各個運營商以獲取他們的后綴,但這里有一個列表(主要是北美運營商)可以幫助您入門:

Name                          Gateway
7-11 Speakout                 @cingularme.com
Alaska Communications Systems @msg.acsalaska.com
Alltel Wireless               @message.alltel.com
American Messaging            @amsmsg.net
AT&T Enterprise Paging        @page.att.net
AT&T Mobility                 @cingularme.com
AT&T Wireless                 @txt.att.net
BeepOne                       @beepone.net
Bell Mobility & Solo Mobile   @txt.bell.ca
Boost Mobile                  @myboostmobile.com
Cellular One                  @mobile.celloneusa.com
Cellular South                @csouth1.com
Centennial Wireless           @cwemail.com
Cingular                      @cingularme.com
Cricket                       @mms.mycricket.com
Fido                          @fido.ca
Globalstar                    @msg.globalstarusa.com
Helio                         @myhelio.com
Illinois Valley Cellular      @ivctext.com
Indiana Paging Network        @ipnpaging.com
Iridium                       @msg.iridium.com
MetroPCS                      @mymetropcs.com
MTS                           @text.mtsmobility.com
Ntelos                        @nteloswireless.com
Page1                         @page1email.com
President's Choice            @txt.bell.ca
ProPage Inc.                  @page.propage.net
Qwest                         @qwestmp.com
Rogers                        @pcs.rogers.com
Rogers Paging                 @paging.rogers.com
Sasktel                       @sms.sasktel.com
Shentel                       @shentel.net
Sprint (Nextel)               @page.nextel.com
Sprint (PCS)                  @messaging.sprintpcs.com
Suncom                        @tms.suncom.com
T-Mobile                      @tmomail.net
Telus Mobility                @msg.telus.com
Thumb Cellular                @sms.thumbcellular.com
Tracfone                      @cingularme.com
Unicel                        @utext.com
US Cellular                   @email.uscc.net
USA Mobility                  @usamobility.net
Verizon                       @vtext.com
Virgin Mobile (Canada)        @vmobile.ca
Virgin Mobile (USA)           @vmobl.com

可以在此處找到更全面的列表: http://en.wikipedia.org/wiki/List_of_carriers_providing_SMS_transit

這種方法要求您的用戶在輸入他們的號碼時指定他們的手機運營商,但是您可以免費向他們發送文本(從您的角度來看),就像在 .NET 中發送 email 一樣。

作為旁注,主題行並不總是被正確解釋。 大多數運營商只是將 email 轉換為類似[Subject] [Body]的短信,但有些運營商完全放棄了 Subject。

也有提供這項服務的公司(當然是收費的)。 最明顯的是Twilio但快速谷歌搜索應該會找到更多。

我推薦Twilio 您不必知道您的用戶擁有哪個運營商,也不必擔心他們稍后會切換運營商並且他們永遠不會收到他們的文本。 使用 Twilio,您可以處理更多邏輯,例如發送文本詢問問題並要求他們回復“y”或“n”。 它也可以用來撥打電話。 還支持 C# 和其他語言。

然而,每篇文字都要花一分錢——所以它不是免費的!
但是,如果您的要求是這樣並且口袋足夠深以處理便士文本,那么我會推薦它們。 我沒有發現任何更便宜的東西,他們會預先給你信用,這樣你就可以在不花任何錢的情況下進行測試。 這一切都是付費的,所以如果你有 30 美元的信用額度,你不必擔心如果你炸毀每個人的蜂鳴器,或者這些天孩子們正在使用的任何東西,你會看到一張 1,000 美元的賬單。

這只是另一種發送文本的方式,我有很好的經驗並且覺得值得注意。
我知道每個人都喜歡免費。

根據克里斯肯特的回答,您可以像這樣發送 email:

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

mail.From = new MailAddress("your_email_address@gmail.com");
mail.To.Add("2128675309@cingularme.com");
mail.Subject = "Test Mail";
mail.Body = "This is for testing SMTP mail from GMAIL";

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
SmtpServer.EnableSsl = true;

SmtpServer.Send(mail);
MessageBox.Show("mail Send");

但是,如果我沒記錯的話,以這種方式發送的 SMS 並不能 100% 保證會出現(盡管很有可能)

我使用了 TcpClient,它實現了 Disposable 並創建了 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();
                        }
                    }
                }
            }
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM