简体   繁体   English

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

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

How do you send an sms message to a mobile phone from a web application using asp.net and c#?如何使用 asp.net 和 c# 从 web 应用程序向手机发送短信?

Most carriers provide an email suffix that can be used to send an SMS through email.大多数运营商提供 email 后缀,可用于通过 email 发送短信。 Generally:一般来说:

[PhoneNumber]@[Suffix]

You can contact individual carriers to get their suffix, but here is a list (Mostly North American carriers) to get you started:您可以联系各个运营商以获取他们的后缀,但这里有一个列表(主要是北美运营商)可以帮助您入门:

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

A more comprehensive list can be found here: http://en.wikipedia.org/wiki/List_of_carriers_providing_SMS_transit可以在此处找到更全面的列表: http://en.wikipedia.org/wiki/List_of_carriers_providing_SMS_transit

This approach requires your users to specify their cell carrier when entering their number, but then you can send them texts for free (from your perspective) the same as sending an email in .NET.这种方法要求您的用户在输入他们的号码时指定他们的手机运营商,但是您可以免费向他们发送文本(从您的角度来看),就像在 .NET 中发送 email 一样。

As a side note, subject lines are not always interpretted correctly.作为旁注,主题行并不总是被正确解释。 Most carriers just convert an email to sms like [Subject] [Body] but some discard the Subject completely.大多数运营商只是将 email 转换为类似[Subject] [Body]的短信,但有些运营商完全放弃了 Subject。

There are also companies that provide this service (for a fee of course).也有提供这项服务的公司(当然是收费的)。 The most obvious is Twilio but a quick Google search should find you some more.最明显的是Twilio但快速谷歌搜索应该会找到更多。

I'd recommend Twilio .我推荐Twilio You don't have to know which carrier your user has or worry about them switching carriers later on and them never receiving their texts.您不必知道您的用户拥有哪个运营商,也不必担心他们稍后会切换运营商并且他们永远不会收到他们的文本。 With Twilio you can process more logic, like sending a text to ask a question and asking them to text back "y" or "n".使用 Twilio,您可以处理更多逻辑,例如发送文本询问问题并要求他们回复“y”或“n”。 It can also be used to make phone calls.它也可以用来拨打电话。 There is support for C# and other languages as well.还支持 C# 和其他语言。

However, each text costs a penny - so it is not free!然而,每篇文字都要花一分钱——所以它不是免费的!
But if your requirements are such and pockets deep enough to handle penny-texts, then I'd recommend them.但是,如果您的要求是这样并且口袋足够深以处理便士文本,那么我会推荐它们。 I haven't found anything cheaper and they give you credit up front so you can test it out without costing you anything.我没有发现任何更便宜的东西,他们会预先给你信用,这样你就可以在不花任何钱的情况下进行测试。 It's all pay-go, so if you have a $30 credit, you don't have to worry about seeing a $1,000 bill should you blow up everybody's beepers, or whatever the kids are using these days.这一切都是付费的,所以如果你有 30 美元的信用额度,你不必担心如果你炸毁每个人的蜂鸣器,或者这些天孩子们正在使用的任何东西,你会看到一张 1,000 美元的账单。

This is just another way to send texts that I have had a good experience with and feel is worth noting.这只是另一种发送文本的方式,我有很好的经验并且觉得值得注意。
I know everybody prefers free.我知道每个人都喜欢免费。

Building off theChrisKent's answer, you could send the email like this:根据克里斯肯特的回答,您可以像这样发送 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");

If I recall correctly however, SMS sent in this fashion are not 100% guaranteed to show up (though it is likely)但是,如果我没记错的话,以这种方式发送的 SMS 并不能 100% 保证会出现(尽管很有可能)

I used TcpClient, which implements Disposable & created SmsHelper class我使用了 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.

相关问题 如何在ASP.Net webForms中从Web应用程序向移动设备发送和接收短信 - How to send and receive sms from web application to mobile in ASP.Net webForms 如何在没有第三方短信门方式的情况下使用asp.net C#从网站发送短信 - How send sms from websites using asp.net c# without third party sms gate way 从C#+ ASP.NET发送免费短信 - send free SMS from C# + ASP.NET 如何从 asp.net mvc 应用程序发送批量短信 - How to send bulk sms from asp.net mvc application 如何使用诺基亚手机从C#应用程序发送/接收短信 - How to Send/Receive SMS Messages from C# application, using Nokia Mobile Phone 使用C#Web应用程序中的MAPI在ASP.NET中使用默认Web客户端发送邮件 - Send mail using default webclient in ASP.NET using MAPI in C# web application c#使用asp.net web应用程序每天通过电子邮件发送上传的文件 - c# send uploaded file through email daily using asp.net web application 将字符串从 c# windows 表单应用程序发送到 asp.net Z2567A5EC9705EB7AC2C984033E06 服务器 - send string from c# windows form application to asp.net web server 在ASP.NET C#Web应用程序中实现SMS发送功能 - Implement SMS sending functionality in ASP.NET C# web application 将XPath与ASP.NET C#Web应用程序一起使用 - Using XPath with ASP.NET C# Web Application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM