Introduction
In this article, I will be sharing a quick and 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 though they keep irritating you with continuous messages regarding their advertisements.
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 using ViaNett. Let’s start without actually wasting time.
Get Started
To start with, visit the ViaNett website (ViaNett Free demo Account). Sign up and register 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:
Click on the Technical API and that will navigate to different kinds of API it provides. Here, we would need 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 image for creating a Windows Form application. Those who are really new to Windows Forms, please follow one of the best links below:
After you create a simple Windows Form application, it would look something like below:
Create as simple as you can, as this is just for learning purposes. 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:
private void button1_Click(object sender, EventArgs e)
{
using (var web = new System.Net.WebClient())
{
try
{
string userName = txtUserName.Text;
string userPassword = txtUserPassword.Text;
string msgRecepient = txtPhNumber.Text;
string msgText = txtMessage.Text;
string url = "http://smsc.vianett.no/v3/send.ashx?" +
"src="+msgRecepient +
"&dst="+msgRecepient +
"&msg="+System.Web.HttpUtility.UrlEncode
(msgText,System.Text.Encoding.GetEncoding("ISO-8859-1")) +
"&username=" + System.Web.HttpUtility.UrlEncode(userName) +
"&password="+userPassword;
string result = web.DownloadString(url);
if(result.Contains("OK")){
MessageBox.Show("Sms sent successfully",
"Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else{
MessageBox.Show("Some issue delivering",
"Message",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
catch (Exception ex)
{
}
}
}
The above code would look simple, rather it 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.
**Point to note is: The country code needs to be prefixed to the phone number, in order to send the message else it will mail back regarding the failure to the registered email.
Conclusion
Thus, this is a small article on integration and sending SMS using C# application. I hope this helps beginners!! I will come up with more articles with other APIs and the other SMS features they support.
References