26

Is there a way to send SMS from an ASP.NET website using a Web API? I know about web services, but don't know how to call those services from my app.

3 Answers 3

35

Web services are the best way to do it. I use Twilio on a site, and it was incredibly easy to get set up and working. Scalability is no issue, and you will more than make up for the cost in not having to spend developer hours building your own solution.

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

Twilio libraries available for .NET: https://www.twilio.com/docs/csharp/install

From the twilio-csharp project, here is the example of how to send an SMS (I took this from twilio-csharp. Just reposting it to show how easy it is)

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);
    }    
}
3
  • 1
    Thanks a lot, will check it out. I have one more doubt, to send the SMS do i have to host the site ? or can i send it using Visual Studio itself?
    – Kanishka
    Mar 29, 2011 at 4:48
  • It doesn't need to be a site. You can do this from any type of .NET assembly. An ASP.NET website hosted or running inside VS, a console application, a windows service, whatever.
    – mfanto
    Apr 4, 2011 at 0:24
  • You can download complete code for Twilio outbound call and SMS in following link envato codecanyon.net/item/click-to-call-and-message-with-aspnet/… Mar 1, 2016 at 13:24
11

I think I am a bit late to tell you that you are in luck, but for those who find this article later, I created a video showing how to send a Text Message using your Twilio account and asp.net:

i walk you through sending a text message using twilio and asp.net c#

In case you don't have 10 minutes to spend watching the video, here is the code:

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);
        }
    }
}

To make this code work you need to nuGet the Twilio API and need to replace the my configurationmanager.appsettings stuff with your account id and auth token.

Happy coding!

2
  • 3
    I might call that number to see if Mark Hagan answers.
    – d512
    Sep 16, 2015 at 14:46
  • I might! Just depends on whether or not I remembered to cancel number on the test account. :D
    – Mark Hagan
    Sep 17, 2015 at 15:11
7

Instead of doing it with Twilio API, if you prefer to do it with another SMS service provider Way2Sms.com I think below code will help you:

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();

    }
}
4
  • Is it possible to send images thru the SMS message?
    – Sam Cromer
    Sep 12, 2014 at 21:43
  • 3
    @SamCromer SMS stands for Short Message Service, pictures can be sent via MMS, or Multimedia Message Service or email. Well this comment is old and you probably know that by now, if you do, disregard my comment.
    – Felype
    May 4, 2015 at 14:33
  • valid for india only i want to send to whole world bro May 20, 2015 at 6:51
  • @Felype your comment is a contribution to the knowledge that the Internet Holds. even if Sam Cromer already knew, there will be many more people to come that did not. +1 Thanks! even though that sort of thing ain't cool on stack.
    – RadioSpace
    Jul 2, 2015 at 1:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.