从ASP.NET网站发送短信

有没有办法使用Web API从ASP.NET网站发送短信? 我知道Web服务,但不知道如何从我的应用程序调用这些服务。

Web服务是最好的方法。 我在一个网站上使用Twilio,这是非常容易build立和工作。 可伸缩性是没有问题的,你将不需要花费开发人员的时间来构build自己的解决scheme。

Twilio: http ://www.twilio.com/

可用于.NET的Twilio库: https : //www.twilio.com/docs/csharp/install

从twilio-csharp项目,这里是如何发送短信的例子(我从twilio-csharp获取这个,只是重新显示它是多么容易)

static void Main(string[] args) { TwilioRestClient client; // ACCOUNT_SID and ACCOUNT_TOKEN are from your Twilio account client = new TwilioRestClient(ACCOUNT_SID, ACCOUNT_TOKEN); var result = client.SendMessage(CALLER_ID, "PHONE NUMBER TO SEND TO", "The answer is 42"); if (result.RestException != null) { Debug.Writeline(result.RestException.Message); } } 

我想我迟到了告诉你,你很幸运,但是对于那些稍后find这篇文章的人,我创build了一个video,展示了如何使用Twilio帐户和asp.net发送文本消息:

我引导你通过使用twilio和asp.net发送短信c#

如果你没有10分钟的时间观看video,这里是代码:

 using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using Twilio; namespace TwilioSMSHowTo { public partial class _default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void SendMessage_OnClick(object sender, EventArgs e) { string ACCOUNT_SID = ConfigurationManager.AppSettings["ACCOUNT_SID"]; string AUTH_TOKEN = ConfigurationManager.AppSettings["AUTH_TOKEN"]; TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); client.SendMessage("(502) 276-8990", ToNumber.Text, Message.Text); } } } 

为了使这个代码工作,您需要nuGet Twilio API,并需要用您的帐户ID和身份validation令牌replace我的configurationmanager.appsettings的东西。

快乐的编码!

而不是Twilio API,如果你喜欢与另一个SMS服务提供商Way2Sms.com做,我想下面的代码将帮助你:

 public void sendsms(object sender, EventArgs e) { if (Page.IsValid) { HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create("http://ubaid.tk/sms/sms.aspx?uid=" + yourmobilenumber + "&pwd=" + yourpassword + "&msg=" + body.Text + "&phone=" + recipientNo.Text + "&provider=way2sms"); HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse(); System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream()); string responseString = respStreamReader.ReadToEnd(); respStreamReader.Close(); myResp.Close(); } }