How to send SMS from C# using HTTP

You can easily build a C# console application that allows you to send a HTTP request to the Ozeki 10 SMS gateway. When you run this attached application, first, it will print a HTTP request on the console and after that send it out. If everything goes well the Ozeki 10 SMS gateway receives this request, and sends back a response. Our C# application receives this response and displaying on the console. From this response we can find out if the delivery was successful.

What is a C# SMS API?

The C# SMS API is a great tool to be able to send SMS message from any kind of C# based project or application by initiating HTTP requests and forwarding them to the SMS Gateway.

Prerequisites

Send SMS from C#

If you wish to send SMS from C#, you can use the HTTP SMS API of Ozeki SMS Gateway.

To send sms from C#:

  1. Open https://localhost:9515 in your browser and log in
  2. Create a new HTTP API user
  3. Check the port number of the HTTP API service
  4. Start Visual Studio
  5. Create a new C# console project
  6. Send sms from C# by typing in the source code bellow
  7. Run the Console App project
  8. View the SMS sent from your C# project on your mobile

How to Send SMS from C# video

C# SMS source code example

The following example C# source code is free to use, you can simply implement it into your project or you can modify the source code to use it for other projects or applications. If you would like to run this example code, you just need to copy-paste it into your Console App project and run the project.

using System;
using System.Net.Http;
using System.Text;
using System.Web;
namespace HttpApiTester
{
    class Program
    {
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            var username = "john";
            var password = "Xc3ffs";
            var messagetype = "SMS:TEXT";
            var httpUrl = "https://127.0.0.1:9508/";
            var recipient = HttpUtility.UrlEncode("+36201324567", Encoding.UTF8);
            var messagedata = HttpUtility.UrlEncode("TestMessage", Encoding.UTF8);
            
            var sendString = $"{httpUrl}api?action=sendmessage&username=" +
                             $"{username}&password={password}" +
                             $"&recipient={recipient}&messagetype=" +
                             $"{messagetype}&messagedata={messagedata}";

            Console.WriteLine("Sending request: " + sendString);

            var handler = new HttpClientHandler();
            handler.ServerCertificateCustomValidationCallback = 
                (sender, cert, chain, sslPolicyErrors) => { return true; };

            using var client = new HttpClient(handler);

            try
            {
                var response = await client.GetStringAsync(sendString);
                Console.WriteLine("Http response received: ");
                Console.WriteLine(response);

            } catch(Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

Step 1 - Open Visual Studio

The first step to create the application that can send SMS message is to open the Visual Studio. If you haven't downloaded Visual Studio yet, just follow the link in the Prerequisites section above to download the latest version. If you have the installed Visual Studio on your computer, all you need to do is to click on its icon (Figure 1) on the desktop to open Visual Studio.

open visual studio
Figure 1 - Open Visual Studio 2019

Step 2 - Create a new project

After you opened Visual Studio, the opening window will show up for you as you can see it in Figure 2. Here you can see your solutions, you can open an existing one, clone a repository or open a local folder. Now, to follow the guide, you need to click on the 'Create a new project' button to create the project for SMS sending.

creating new project in visual studio
Figure 2 - Creating new project

Step 3 - Select Console App

The next window lists all the available types of projects that can be created in Visual Studio. This example requires you to create a simple Console App as Figure 3 demonstrates it, but you can use the SMS sending service in many other types of project. So, all you have to do here is to click on the Console App option from the list of available types of projects.

choosing console application
Figure 3 - Choosing Console Application

Step 4 - Configure the project

Before crating the project, the final step is to configure it by specifying some details of the project. Here, you can give a name to the project and also set the location folder as well as you can see it in Figure 4. If you finished with the configuration, you can just click on 'Create' to create the Console App project.

setup visual studio project
Figure 4 - Setup Visual Studio project

Step 5 - Copy the example code

After you created the Console App project, now you need to set up the example code that you need to execute. For that, scroll up on this page to find the example code section and mark out the whole source code as Figure 5 shows that. Then just press Ctrl+C on your keyboard to copy the source code.

copy code from website
Figure 5 - Copy code from website

Step 6 - Paste the source code into your project

In the Visual Studio, the created project contains an initial Program.cs source file. At this point, this is the file that you have to use to execute the example program. Here, first, mark out the code in that file and delete it. After that, you Figure 6 demonstrates it, press Ctrl+V on your keyboard to paste the example code into your Program.cs file. Now, the project is ready to use.

paste code into visual studio
Figure 6 - Paste code into Visual Studio

Step 7 - Execute the example project

The final step of this guide is to execute the example program. In Visual Studio, it is quite simple to run the project, you just need to click on the Run button as you can see it in Figure 7. After starting the program, it shows up a console window, that prints the HTTP request that the program sent to the SMS Gateway. It also prints the response from the SMS Gateway that shows if the delivery of the SMS was successful.

build and run your code in visual studio
Figure 7 - Build and run your code in Visual Studio

Step 8 - Check the send result in the Ozeki log

In Ozeki 10 SMS Gateway, you can check easily what messages sent by your application, since the HTTP API service logs every event that occurred during the time it is enabled. So, if you open the SMS Gateway, and select details of the HTTP API service, you will be able to see the events. As you can see it in Figure 8, the service logged an event, when the C# example program sent the HTTP request to the service.

check the logs of the http api service
Figure 8 - Check the logs of the HTTP API Service

The process of the message sending can be also viewed back by the events. For that, open the HTTP API User connection, that you had to configure before. Figure 9 demonstrates that how the connection handles the HTTP request and send the message to the recipient that you specified in your C# program.

check the logs of the http api user
Figure 9 - Check the logs of the HTTP API User

Conclusion

This guide provided all the necessary information on how to send an SMS to any recipient from your computer. This solution uses the combination of C# and the Ozeki SMS Gateway, which works in any country and can send and receive SMS through various mobile connections. This will make the communication with your co-workers and your clients of your organization much easier.

The Ozeki SMS Gateway is a tool that can be controlled using HTTP requests. To learn more about those, feel free to visit the article about receiving SMS using the C# HTTP SMS API, or get info about other supported languages, like PHP.

All you need to do is to download the Ozeki SMS Gateway and start building your solution now!

More information