Click here to Skip to main content
14,636,439 members
Articles » Mobile Development » Android » General
Article
Posted 29 Dec 2014

Stats

141.8K views
9.7K downloads
100 bookmarked

SMS Gateway using Android Phone

Rate this:
4.77 (33 votes)
Please Sign up or sign in to vote.
4.77 (33 votes)
4 Jan 2015CPOL
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.

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,

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)

Share

About the Author

mengwangk
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

 
QuestionIt show public ip not local ip Pin
banhthanhvi29-Mar-15 21:40
Memberbanhthanhvi29-Mar-15 21:40 
AnswerRe: It show public ip not local ip Pin
mengwangk30-Mar-15 0:33
Membermengwangk30-Mar-15 0:33 
GeneralRe: It show public ip not local ip Pin
banhthanhvi30-Mar-15 1:44
Memberbanhthanhvi30-Mar-15 1:44 
GeneralRe: It show public ip not local ip Pin
mengwangk30-Mar-15 18:14
Membermengwangk30-Mar-15 18:14 
GeneralMy vote of 5 Pin
Kamlesh R. Sanchla11-Jan-15 18:25
MemberKamlesh R. Sanchla11-Jan-15 18:25 
QuestionCan you please provide this code in vb.net Pin
Kamlesh R. Sanchla11-Jan-15 18:04
MemberKamlesh R. Sanchla11-Jan-15 18:04 
GeneralMy vote of 5 Pin
Renju Vinod1-Jan-15 17:02
professionalRenju Vinod1-Jan-15 17:02 
QuestionCan you check why this App is crashing again and again Pin
Abhishek Kumar Goswami30-Dec-14 1:28
professionalAbhishek Kumar Goswami30-Dec-14 1:28 
Article is nice , well explained... so thanks for this Article

Can you check Mobile app, its again and again crashed so not able to run......
Abhishek Goswami

AnswerRe: Can you check why this App is crashing again and again Pin
mengwangk30-Dec-14 2:11
Membermengwangk30-Dec-14 2:11 
GeneralRe: Can you check why this App is crashing again and again Pin
Abhishek Kumar Goswami30-Dec-14 4:27
professionalAbhishek Kumar Goswami30-Dec-14 4:27 
GeneralRe: Can you check why this App is crashing again and again Pin
mengwangk1-Jan-15 20:05
Membermengwangk1-Jan-15 20:05 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun29-Dec-14 22:22
MemberHumayun Kabir Mamun29-Dec-14 22:22 

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.