Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#

Send SMS Using ViaNett in C# Application

Rate me:
Please Sign up or sign in to vote.
4.84/5 (26 votes)
5 Apr 2016CPOL3 min read 48.7K   1.4K   35   23
A quick and interesting way to send SMS using a simple C# application

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.

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 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:

2

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:

3

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:

C#
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

This article was originally posted at http://surajpassion.in/send-sms-using-c-application

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionsms sent successfully but didn't receive yet Pin
Member 1133130818-Jul-18 8:20
Member 1133130818-Jul-18 8:20 
QuestionSend SMS Using ViaNett in C# Pin
Member 1308798627-Mar-17 10:05
Member 1308798627-Mar-17 10:05 
QuestionThere is no encryption on your login details Pin
Keithsw8-Apr-16 23:43
Keithsw8-Apr-16 23:43 
AnswerRe: There is no encryption on your login details Pin
Passion4Code9-Apr-16 1:09
professionalPassion4Code9-Apr-16 1:09 
SuggestionNice Simple Send Project... Pin
marsman_CA7-Apr-16 9:31
professionalmarsman_CA7-Apr-16 9:31 
GeneralRe: Nice Simple Send Project... Pin
Passion4Code7-Apr-16 17:58
professionalPassion4Code7-Apr-16 17:58 
GeneralMy vote of 4 Pin
Santhakumar M6-Apr-16 4:57
professionalSanthakumar M6-Apr-16 4:57 
GeneralMy vote of 2 Pin
Mladen Borojevic5-Apr-16 11:19
professionalMladen Borojevic5-Apr-16 11:19 
this is how to use ViaNett API and not how to send sms
GeneralRe: My vote of 2 Pin
Passion4Code5-Apr-16 18:27
professionalPassion4Code5-Apr-16 18:27 
GeneralRe: My vote of 2 Pin
Mladen Borojevic5-Apr-16 20:28
professionalMladen Borojevic5-Apr-16 20:28 
GeneralRe: My vote of 2 Pin
Passion4Code6-Apr-16 0:24
professionalPassion4Code6-Apr-16 0:24 
GeneralRe: My vote of 2 Pin
Mladen Borojevic6-Apr-16 2:31
professionalMladen Borojevic6-Apr-16 2:31 
GeneralRe: My vote of 2 Pin
Passion4Code6-Apr-16 2:47
professionalPassion4Code6-Apr-16 2:47 
GeneralRe: My vote of 2 Pin
dwight100007-Apr-16 8:11
dwight100007-Apr-16 8:11 
QuestionUpload sample code! Pin
Santosh Kokatnur5-Apr-16 1:13
Santosh Kokatnur5-Apr-16 1:13 
AnswerRe: Upload sample code! Pin
Passion4Code5-Apr-16 2:40
professionalPassion4Code5-Apr-16 2:40 
GeneralRe: Upload sample code! Pin
Santosh Kokatnur5-Apr-16 2:43
Santosh Kokatnur5-Apr-16 2:43 
QuestionMissing images Pin
Maverick20094-Apr-16 1:50
professionalMaverick20094-Apr-16 1:50 
AnswerRe: Missing images Pin
Passion4Code4-Apr-16 2:39
professionalPassion4Code4-Apr-16 2:39 
GeneralRe: Missing images Pin
kiquenet.com4-Apr-16 9:33
professionalkiquenet.com4-Apr-16 9:33 
GeneralRe: Missing images Pin
Passion4Code4-Apr-16 21:46
professionalPassion4Code4-Apr-16 21:46 
GeneralRe: Missing images Pin
Maverick20094-Apr-16 23:09
professionalMaverick20094-Apr-16 23:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.