Sometimes, you want to do something that more than c# can do. In one of my projects, a deal bargain website requires to send out text messages to alert their members when pre-defined keywords match with the new deal submitted. It’s a great use of text messages alert to shopper who are looking for a particular keyword in the deal.
So I did quick research online and found you can either try to write your own AT command to communicate your cell phone or you can simply start sending text messages right away using SMS gateway API which is this tutorial going to discuss further.
There are quite a few SMS gateway provider you can find over internet. In this article, we found Gateway160 SMS Gateway, a North America based SMS gateway provider which provides friendly .Net framework integration includes the sample code in C#,VB.Net and legacy ASP.
Get Started
To connect to their API, you need to obtain an account name and API key from them. All you need to do is sign up for an account, it’s free and come with 25 Free SMS credits when your account is verified.
The send text message API is simply a HTTP Post request, and here are the mandatory parameters to fill before calling the API. To find out more options or return codes, please visit their SMS Gateway API documentation
- accountName : the account name when you sign up
- key : the API key
- phoneNumber : the number includes area code
- countryCode : this is a 2 letters size long country code
- message : the content of the text messsage. URL encoded is required
Start Coding
string URLString = "http://api.gateway160.com/client/sendmessage"; string[] param = new string[] { "nameOfMyAccount", "key", "8970098765","US", Url.Encode("Hello world"), "0" }; string postData = string.Format("accountName={0}&key={1}&phoneNumber={2}&countryCode={3}&message={4}&isUnicode={5}", param); byte[] byteArray = Encoding.UTF8.GetBytes(postData); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URLString); request.Method = "post"; request.ContentType = "application/x-www-form-urlencoded"; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (HttpStatusCode.OK == response.StatusCode) { Stream receiveStream = response.GetResponseStream(); StreamReader sr = new StreamReader(receiveStream); string output = sr.ReadLine(); response.Close(); if (output == "1") { // Text message is now delivery to their API and will be sent out in a few seconds. } else if(output == "0") { // Your account is out of SMS Credits, need to purchase more } else { //error } }
The http response output will show the string “1” when the API call is successful and the message will be sent out from their gateway in a few seconds. You can simply take the code above which is a modified version from their c# code sample of sending text messages and plug it into your application.
Imporvements
There are many places we can utilize the text message in real life application such as performing account validation by text messages or appointment remainder, and the advantage of using the text message compares to email is the accuracy and timeliness. It’s a great way to reach real person at real time. I hope I can demonstrate more use of text message in the upcoming tutorials. Enjoy.