【问题标题】:how to send sms to mobile from web application using asp.net and c#?如何使用 asp.net 和 c# 从 Web 应用程序向手机发送短信?
【发布时间】:2017-07-25 04:49:58
【问题描述】:

如何使用 asp.net 和 c# 从 Web 应用程序向手机发送短信?

【问题讨论】:

  • 请提供更多详细信息。
  • 这个问题对我来说似乎很清楚,而且范围足够小以至于可以回答(除非它以某种方式需要代码页来执行此操作)。我稍微清理了一下措辞,并投了赞成票。

标签: c# asp.net


【解决方案1】:

大多数运营商都提供可用于通过电子邮件发送 SMS 的电子邮件后缀。一般:

[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,但快速谷歌搜索应该会找到更多。

【讨论】:

  • 我猜对 API 进行了一些解释,但并没有真正说明如何使用代码发送 SMS 消息。
  • @Robert Harvey 我的回答回答了解决方案的方式(架构)。通过代码发送电子邮件非常简单,可以在 SO 和其他地方的多个位置找到,因此它似乎不太相关。事实上,如果 OP 不想搜索,@krtego 的答案应该可以很好地填补空白。
  • 啊,我明白了。我猜我没猜到这只是一封普通的电子邮件。
【解决方案2】:

我推荐Twilio。您不必知道您的用户拥有哪个运营商,也不必担心他们稍后会切换运营商并且他们永远不会收到他们的文本。使用 Twilio,您可以处理更多逻辑,例如发送文本以提出问题并要求他们回复“y”或“n”。它也可以用来拨打电话。也支持 C# 和其他语言。

但是,每条文本都需要花费一美分 - 所以它不是免费的!
但是,如果您的要求如此且口袋足够深以处理便士文本,那么我会推荐它们。我没有发现任何更便宜的东西,他们预先给你信用,这样你就可以在不花任何钱的情况下测试它。这一切都是付费的,所以如果您有 30 美元的信用额度,您不必担心如果您炸毁每个人的蜂鸣器或这些天孩子们正在使用的任何东西,就会看到 1,000 美元的账单。

这只是发送文本的另一种方式,我有很好的经验并且觉得值得注意。
我知道每个人都喜欢免费。

【讨论】:

    【解决方案3】:

    根据克里斯肯特的回答,您可以这样发送电子邮件:

    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% 保证会出现(尽管很有可能)

    【讨论】:

      【解决方案4】:

      我使用了 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();
                              }
                          }
                      }
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-05
        • 2017-04-03
        • 1970-01-01
        • 2012-03-18
        • 2012-11-10
        • 2012-03-12
        • 1970-01-01
        相关资源
        最近更新 更多