How to send SMS messages in C#

By FoxLearn 7/16/2024 8:36:53 AM   19.74K
To send SMS messages to mobile phones via the internet using the Vianett API in a C# Windows Forms Application, you'll need to follow these steps.

ViaNett is a sms gateway that helps you send text message from c# application to mobile phone via internet.

How to send SMS Message in C#

To send an SMS message in C# using the ViaNett API you need to sign up for an account on the ViaNett website and obtain your API credentials.

Design a simple form that allows you to enter phone number, message, then click the button to send an sms

Open your form designer, then drag and drop Label, TextBox and Button controls from the Visual Studio toolbox to your winform, then layout your UI as shown below.

send text message from c# application

Add the button click event handler to the Send button allows you to send a sms message to mobile or cellphone using sms gateway as the following c# code.

// c# send sms message
private void btnSend_Click(object sender, EventArgs e)
{
   // send sms c#
    using (System.Net.WebClient client = new System.Net.WebClient())
    {
        try
        {
            string url = "http://smsc.vianett.no/v3/send.ashx?" +
                "src=" + txtPhoneNumber.Text + "&" +
                "dst=" + txtPhoneNumber.Text + "&" +
                "msg=" + System.Web.HttpUtility.UrlEncode(txtMessage.Text, System.Text.Encoding.GetEncoding("ISO-8859-1")) + "&" +
                "username=" + System.Web.HttpUtility.UrlEncode(txtUsername.Text) + "&" +
                "password=" + System.Web.HttpUtility.UrlEncode(txtPassword.Text);
            //Call web api to send sms messages
            string result = client.DownloadString(url);
            if (result.Contains("OK"))
                MessageBox.Show("Your message has been successfully sent.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("Message send failure.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

You can you the WebClient class to call c# send text message through vianett sms gatewate api, then get the result value returned from the api method. You can also use the code above to learn how to send sms using c# web application.

To play demo you need to create an account at vianett website, then you will get 5 free sms. ViaNett is working hard to ensure that any developer who wants to use the messaging gateway in their application, site or system can do so reliably and simply.

Through the c# example, you learned how to send free sms using c# windows application or how to send sms using api in c# net.

VIDEO TUTORIAL