Introduction 

This article gives you information on how to send sms from a C# software using your android mobile phone. The best thing about this solution, is that it allows you to take advantage of great, often free SMS tariffs provided by mobile networks. For example if you purchase a SIM card in the UK from Vodafone or O2, you get unlimited texts. On the other hand if you want to use an online SMS service provider, SMS sending will be very costly.

Download: send-sms-from-c-sharp-on-android.zip (50.15Kb)

How to send sms from c# using an android phone 

  1. Install the Android SMS Gateway app on your mobile
  2. Create an OZX user in the Android SMS gateway app
  3. Connect to the APP from your C# application over wifi
  4. Send an sms from C#

Figure 1 - Send SMS from C# on android

Where can I find the Android SMS Gateway app

The Android SMS Gateway app can be downloaded from https://android-sms-gateway.com. It requires Android 9.0, and it is a good idea to use a powerful Android phone with minimum 4GB or RAM (6 or 8 is better), to make sure the Android memory manager doesn't kill the service.

The Android sms gateway will run as a service on your Android phone, and will allow you to create an OZX user account. Creating an OZX user account is straightforward, but if you need help, you can follow the steps presented at this link: How to setup an OZX user account. The OZX user account is needed because the c# sms api we use communicates with the SMS gateway using the OZX protocol. When you create an OZX user account, you define a username and password.

On the android phone create a user account with these details:
username: testuser
password: testpass

How to connect to the Android Mobile

To connect your C# sms client to your Android mobile, you need to create an OzxClient object. After this object is created, you can login to the IP address of the mobile phone through wifi. During the connection, you must give the username and password you have created on the Android phone. Here is the code used to connect:

Client = new OzxClient();
Client.Connect("127.0.0.1",9580,"testuser","testpass");

The connection is done asynchrnously, so it is a good idea to sign up for the connection successful and connection failed events:

Client.OnConnected += Client_OnConnected;
Client.OnDisconnected += Client_OnDisconnected;

You should sign up to these events before you call the connect method.

How to send an SMS from C#

To send an SMS from C# using the Android mobile, you can use the following example. If you analyze this example closely, you will see, that you need to create an OzxClient object as explained before, and you need to sign up for the messaging events. After the connection is done, in the OnConnected method you can send your first sms using the Client.Send

Note that to send an SMS you need to create an instance of the OzxMessage class. When you create this class, you can define various sms related parameters. In the example, to keep it short we only set the recipient phone number and the message text.

When you create the message, it is a good idea to save the message ID. This id is used in the events. The reason why this SMS api is better than others is because it provides information about what happens to your SMS. You will get feedback when the SMS was accepted for delivery, when the sms was submitted to the gsm network and when it was received by the recipient. Of course if an error happens it is also reported. All of these events use the message ID you get when you create an instance of the OzxMessage class.

using System;
using OZX;

namespace OzekiConsoleClient
{
    class Program
    {
        static OzxClient Client;

        static void Main(string[] args)
        {
         	Client = new OzxClient();
            Client.AutoReconnect = true;

            Client.OnMessageAcceptedForDelivery += Client_OnMessageAcceptedForDelivery;
            Client.OnMessageNotAcceptedForDelivery += Client_OnMessageNotAcceptedForDelivery;
            Client.OnMessageSubmitSuccess += Client_OnMessageSubmitSuccess;
            Client.OnMessageSubmitFailed += Client_OnMessageSubmitFailed;
            Client.OnMessageDeliverySuccess += Client_OnMessageDeliverySuccess;
            Client.OnMessageDeliveryFailed += Client_OnMessageDeliveryFailed;
            Client.OnMessageViewed += Client_OnMessageViewed;
            Client.OnConnected += Client_OnConnected;
            Client.OnDisconnected += Client_OnDisconnected;
        
            Client.Connect("127.0.0.1",9580,"testuser","testpass");
        }

        static void Client_OnConnected(object sender, EventArgs e)
        {
            Console.WriteLine("Successfully connected.");
            
            var msg = new OzxMessage();
            msg.ToAddress = recipient;
            msg.Text = message;

            Console.WriteLine("Sending message. ID: "+msg.ID);
            Client.Send(msg);
        }

        static void Client_OnDisconnected(object sender, OzxArgs< string > e)
        {
            Console.WriteLine("Connection closed. Reason: " + e.Item.ToString());
        }

        static void Client_OnMessageAcceptedForDelivery(object sender, OzxArgs< string > e)
        {
            Console.WriteLine("Message accepted for delivery. ID: " + e.Item.ToString());
        }

        static void Client_OnMessageNotAcceptedForDelivery(object sender, OzxArgs< string, string > e)
        {
            Console.WriteLine("Message rejected. ID: " + e.Item1.ToString()+" Reason: "+e.Item2);
        }

        static void Client_OnMessageSubmitSuccess(object sender, OzxArgs< string, DateTime > e)
        {
            Console.WriteLine("Message submitted. ID: "+e.Item1+" Date: "+e.Item2);
        }

        static void Client_OnMessageSubmitFailed(object sender, OzxArgs< string, DateTime, string > e)
        {
            Console.WriteLine("Message submit failed. ID: " + e.Item1 + " Date: " + e.Item2+" Reason: "+e.Item3);
        }

        static void Client_OnMessageDeliverySuccess(object sender, OzxArgs< string, DateTime > e)
        {
            Console.WriteLine("Message delivered. ID: " + e.Item1 + " Date: " + e.Item2);
        }

        static void Client_OnMessageDeliveryFailed(object sender, OzxArgs< string, DateTime, string > e)
        {
            Console.WriteLine("Message delivery failed. ID: " + e.Item1 + " Date: " + e.Item2 + " Reason: " + e.Item3);
        }

        static void Client_OnMessageViewed(object sender, OzxArgs< string, DateTime > e)
        {
            Console.WriteLine("Message viewed. ID: " + e.Item1 + " Date: " + e.Item2);
        }
    }
}

How to test this code?

The most obvious way to test this code is to login to the sms gateway gui on your Android mobile and check the logs. The events you have subscribed for can also be used for feedback. For example if you cannot connect the OnDisconnected event will be raised. If the SMS is not sent for whatever reason the OnMessageDeliveryFailed event will be triggered.

To sum it up

The above code sample shows you how easy it is to send an sms from your Visual Studio environment through your Android mobile. To get a more detailed explaination, it might be a good idea to read the how to send sms from C# article. The above example was written as a console app. You can use the same code in Windows form or WPF apps. The reason I think this solution is great, is because this c# sms client is very good for windows services. Since it maintains a permanent link between the mobile phone and my service, and it notifies me whenever an SMS delivery report or an incoming SMS arrives. You will also like the fact, that Ozeki, offers the full source code of their API free of charge under the MIT license, so basically you can do whatever you want with it.