Click here to Skip to main content
15,867,568 members
Articles / Mobile Apps / Android

SMS Gateway using Android Phone

Rate me:
Please Sign up or sign in to vote.
4.77/5 (33 votes)
4 Jan 2015CPOL2 min read 184.4K   10.8K   101   56
Turn your Android phone into a SMS gateway, sending and receiving SMS through RESTful service

Introduction

With Internet and the variety of mobile messaging app available, nowadays people can communicate through many channels, be it through email, WhatsApp, LINE, WeChat, Facebook Messenger, Skype, Telegram and many others. However, SMS is still relevant due to its reliability. In this article, we will turn an Android phone into an SMS gateway by installing a free app and start sending and receiving SMS through C# or any other programming languages that you prefer.

Background

I developed this solution to bundle it with my other product (MessagingToolkit). This free Android app is now available at Google Play Store

Of course there are other similiar solutions available, just Google "Android SMS gateway" and you can see all other options available.

Disclaimer: I am the developer for myMobKit and MessagingToolkit

Using the code

In order to turn your Android phone into a SMS gateway, you will need to install the free app myMobKit available at Google Play Store. 

After installing, start the control panel service, and you should be able to see the URL to access the hosted website.

Image 1

 

The hosted website shows the available APIs and their usage. You can use the APIs to access device information, photos and video, as well as send and receive SMS. 

Image 2

 

To get started quickly, you can use Chrome extensions like Advanced REST Client or Postman - REST Client to access the messaging services. Below is a screen capture of Advanced REST Client retrieveing all the SMS in the phone.

 

Image 3

 

Using C# to access the APIs is straightforward using ASP.NET Web API client library.

In Visual Studio, from the Tools menu, select Library Package Manager, then select Package Manager Console.

In the Package Manager Console window, type the following command:

Install-Package Microsoft.AspNet.WebApi.Client

To retrieve all messages in the phone, use the following code snippet.

C#
using (var client = new HttpClient())
{
    string url = ConstructBaseUri();
    client.BaseAddress = new Uri(url);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                    "Basic",
                     Convert.ToBase64String(
                     ASCIIEncoding.ASCII.GetBytes(
                     string.Format("{0}:{1}", txtUserName.Text, txtPassword.Text))));
    }

    HttpResponseMessage response = await client.GetAsync(MessagesUrlPath);
    if (response.IsSuccessStatusCode)
    {
        GetMessageResponse result = await response.Content.ReadAsAsync<GetMessageResponse>();
        if (result.IsSuccessful)
        {
            txtOutput.Clear();
            foreach (DeviceMessage msg in result.Messages)
            {
                AddToOutput(msg.ToString());
                AddToOutput("");
            }
        }
        else
        {
            MessageBox.Show(result.Description, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    else
    {
        MessageBox.Show(response.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

To send a message, use the following code snippet,

C#
using (var client = new HttpClient())

{

    string url = ConstructBaseUri();
    client.BaseAddress = new Uri(url);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    if (!string.IsNullOrEmpty(txtUserName.Text) && !string.IsNullOrEmpty(txtPassword.Text))
    {
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                    "Basic",
                     Convert.ToBase64String(
                     ASCIIEncoding.ASCII.GetBytes(
                     string.Format("{0}:{1}", txtUserName.Text, txtPassword.Text))));
    }

    var postData = new List<KeyValuePair<string, string>>();
    postData.Add(new KeyValuePair<string, string>("to", txtContact.Text));
    postData.Add(new KeyValuePair<string, string>("message", txtMessage.Text));
    HttpContent content = new FormUrlEncodedContent(postData); 

    HttpResponseMessage response = await client.PostAsync(MessagesUrlPath, content);
    if (response.IsSuccessStatusCode)
    {
        PostMessageResponse result = await response.Content.ReadAsAsync<PostMessageResponse>();
        if (result.IsSuccessful)
        {
            txtOutput.Clear();
        }
        else
        {
            MessageBox.Show(result.Description, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    else
    {
        MessageBox.Show(response.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

The screenshot of the sample application.

Image 4

 

History

December 29, 2014 - Initial version.

 

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)
Malaysia Malaysia
A programmer for a long time, and still learning everyday.

A supporter for open source solutions, and have written quite a few open source software both in .NET and Java.

https://mengwangk.github.io/

Comments and Discussions

 
QuestionUpdate Sms read status Pin
Member 1454291519-May-22 9:42
Member 1454291519-May-22 9:42 
QuestionHow to send sms through Android Mobile in c# Pin
Member 1464246921-Dec-19 21:14
Member 1464246921-Dec-19 21:14 
AnswerMessage Closed Pin
24-Jul-20 12:48
Farhan198424-Jul-20 12:48 
Questionsend sms doesn't seem to work Pin
Glenn Boc23-Jun-19 16:59
Glenn Boc23-Jun-19 16:59 
QuestionIs there a way to get this code for vb.net (please) Pin
savedlema9-Feb-19 8:39
savedlema9-Feb-19 8:39 
QuestionConnect application with public IP on the mobile Pin
ritmas17-Jan-19 6:40
ritmas17-Jan-19 6:40 
QuestionSend 2 sms in one request?? Pin
Member 128615806-Nov-18 21:24
Member 128615806-Nov-18 21:24 
QuestionSender Alias? Pin
jimissama18-May-18 4:02
jimissama18-May-18 4:02 
General... Pin
Member 1375423529-Mar-18 20:58
Member 1375423529-Mar-18 20:58 
QuestionSend SMS through attached USB cable Pin
Tarun_SJS12-Feb-18 18:04
Tarun_SJS12-Feb-18 18:04 
QuestionUsing API in my Project Pin
Haidar ali30-Dec-17 20:23
Haidar ali30-Dec-17 20:23 
QuestionSMS does not need Interent Pin
Member 1027179610-Jun-17 19:08
Member 1027179610-Jun-17 19:08 
AnswerRe: SMS does not need Interent Pin
mengwangk27-Jun-17 22:51
mengwangk27-Jun-17 22:51 
QuestionSMS url Pin
Rolfio_21-Mar-17 8:56
Rolfio_21-Mar-17 8:56 
BugDeliveryReport not always work (myMobKit) Pin
Member 130485479-Mar-17 2:30
Member 130485479-Mar-17 2:30 
Questiondual sim send sms Pin
Shatoot Services27-Nov-16 5:40
Shatoot Services27-Nov-16 5:40 
Questionwant to know how to retrieve call log on windows application using mymobkit Pin
Member 1274492229-Sep-16 1:49
Member 1274492229-Sep-16 1:49 
QuestionMessage sent but distination the TEXT MESSAGE Pin
nater3039-Sep-16 16:22
nater3039-Sep-16 16:22 
I installed mymobkit on andriod and set up all what explained and i used your project C# , the message looks it sent but the destination didnt recieve the TEXT MESSAGE.
Any help ?

Thank you
AnswerRe: Message sent but distination the TEXT MESSAGE Pin
mengwangk11-Sep-16 20:04
mengwangk11-Sep-16 20:04 
GeneralRe: Message sent but distination the TEXT MESSAGE Pin
Ianina R12-Sep-18 13:53
Ianina R12-Sep-18 13:53 
Questionchange ip Pin
elaion2721-Jul-16 4:57
elaion2721-Jul-16 4:57 
AnswerRe: change ip Pin
OriginalGriff21-Jul-16 5:00
mveOriginalGriff21-Jul-16 5:00 
QuestionSending message with php Pin
Member 1263936617-Jul-16 6:56
Member 1263936617-Jul-16 6:56 
QuestionError while deleting a sms Pin
siavashebrahimi8-May-16 3:27
siavashebrahimi8-May-16 3:27 
Questionsend 5 thousands POST request to this app asynchronously Pin
siavashebrahimi6-May-16 22:29
siavashebrahimi6-May-16 22:29 

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.