2011-05-24 137 views
2

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

+2

請提供更多的細節。 – 2011-05-24 16:23:25

+0

這個問題似乎對我來說足夠清楚,並且範圍足夠小以致可以負責(除非它以某種方式需要頁面代碼來執行此操作)。我清理了一下這個詞,然後提高了。 – 2011-05-24 16:49:58

回答

6

大多數運營商都提供一個電子郵件後綴,可用於通過電子郵件發送短信。一般:

[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中。

作爲一個側面說明,主題行不總是正確解釋。大多數運營商只是將電子郵件轉換爲短信,如[Subject] [Body],但有些則完全放棄了主題。

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

+0

我解釋API的種類,我想,但並沒有真正多說如何使用代碼發送SMS消息。 – 2011-05-24 16:51:06

+0

@Robert Harvey我的回答回答瞭解決方案的方式(體系結構)。通過代碼發送電子郵件非常簡單,可以在SO和其他地方的多個位置找到,因此它看起來不太相關。事實上,如果OP不想搜索,@ krtego的答案應該填補空白。 – theChrisKent 2011-05-24 20:41:42

+0

啊,我明白了。猜猜我沒有想到,這只是一個普通的電子郵件信息。 – 2011-05-24 20:47:14

2

建立關theChrisKent的回答,您可以發送電子郵件這樣的:

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

mail.From = new MailAddress("[email protected]"); 
mail.To.Add("[email protected]"); 
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"); 

如果我沒有記錯然而,以這種方式發來的短信都不能100%保證顯示出來(雖然它是可能的)

3

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

但是,每個文本花費一分錢 - 所以它是不是免費!
但是,如果你的要求是這樣的,口袋足夠深處理一分錢文本,那麼我會推薦他們。我還沒有找到便宜的東西,他們先給你信譽,所以你可以測試它,而不用花費任何東西。這是全部付出的,所以如果你有30美元的信用額度,你不必擔心看到1000美元的賬單,如果你炸燬每個人的侍者,或者現在孩子們正在使用的東西。

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

0

我用TcpClient的,它實現了一次性&創建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(); 
         } 
        } 
       } 
      } 
     } 
    } 
} 
相關問題