Sending SMS Using C# Application

Introduction

In this article I will be sharing a quick and an interesting way to send SMS using a simple C# application. I will be using ViaNett API to send messages from my application. I will be creating a simple windows form which will trigger the API and send the SMS.

Sending SMS has become a common objective for almost every site nowadays, even though they keep irritating you with continuous messages regarding their advertisements.
1
We use a simple REST API which is provided by various providers in the market. You can try out some of them using the free trial version and send SMS. The reliable and faster one can be chosen. Also check for the Support the team provides.

I have thrown my hands at ViaNett and Twilio APIs. Here, I will be discussing ViaNett. Let's start without  wasting time.

Get Started

To start with, visit the ViaNett website (ViaNett Free demo Account). Sign up and register you with your email address for the free account, which will provide you with 5 free SMS.

After that, you land on a page having contents like below,

2
Click on the Technical Api and that will navigate to the different kinds of API it provides. We would need here the HTTP API in order to send the SMS, as we would be using the WebClient in order to use the API. We will be seeing the code below.

After you click on the HTTP API, you will be navigated to another page which will have the API listed under an example like below,

Create a script to generate a URL like this: http://smsc.vianett.no/v3/send.ashx

src=xxxxx&dst=xxxxx&msg=Hello+world&username=xxxxx&password=xxxxx Retrieve this web page from your script/application, and the SMS will be sent.

This is the API URL which our Web client will be using.

Now to start with our demo application, we will be creating a windows form application. Now, I will not be writing up and lengthening my article with the step by step images for creating a Windows Form application. Those who are really new to windows form, please follow one of the best links below.

After you create a simple Windows Form application, it will look something like below:

3
Create it as simply as you can, as this is just for learning purpose. The UserName is the user name you had provided during the ViaNett registration i.e. the email address

The password is the password you had given during the registration in the ViaNett. Then for the button click we have the following code:

  1. private void button1_Click(object sender, EventArgs e)  
  2. {  
  3.     using(var web = new System.Net.WebClient())   
  4.     {  
  5.         try  
  6.         {  
  7.             string userName = txtUserName.Text;  
  8.             string userPassword = txtUserPassword.Text;  
  9.             string msgRecepient = txtPhNumber.Text;  
  10.             string msgText = txtMessage.Text;  
  11.             string url = "http://smsc.vianett.no/v3/send.ashx?" +  
  12.                 "src=" + msgRecepient +  
  13.                 "&dst=" + msgRecepient +  
  14.                 "&msg=" + System.Web.HttpUtility.UrlEncode(msgText, System.Text.Encoding.GetEncoding("ISO-8859-1")) +  
  15.                 "&username=" + System.Web.HttpUtility.UrlEncode(userName) +  
  16.                 "&password=" + userPassword;  
  17.             string result = web.DownloadString(url);  
  18.             if (result.Contains("OK"))   
  19.             {  
  20.                 MessageBox.Show("Sms sent successfully""Message", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  21.             } else  
  22.             {  
  23.                 MessageBox.Show("Some issue delivering""Message", MessageBoxButtons.OK, MessageBoxIcon.Error);  
  24.             }  
  25.         } catch (Exception ex)   
  26.         {  
  27.             //Catch and show the exception if needed. Donot supress. :)  
  28.   
  29.         }  
  30.     }  
  31. }  

The above code will look simple; it rather is.

When you select a Windows Form Application, by default System.Web is not added to the reference list. Thus, you need to add that from the assembly list.

System.Web is required as we add the WebClient to use the API and download the response.

Here in the code, just giving a brief on what this snippet intends:

We retrieve each text input from the user, i.e. the User name, User password, Text Message & the User Phone Number. We use these as the URL params to the Service API of the ViaNett.

Then after that we will check for the response after sending the message and accordingly we update the User.

Note: The country code needs to be prefixed to the phone number in order to send the messageor  else it will mail back regarding the failure to the registered email.

Conclusion

Thus, this is a small article on the integration and sending SMS using C# aplication. I hope this helps beginners!! I will come up with more articles with other APIs and other SMS features they support.

References

Up Next
    Ebook Download
    View all
    Learn
    View all