Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Send SMS and make Calls with MyPhoneExplorer

0.00/5 (No votes)
2 May 2014 3  
An easy way to send SMS and make Phone calls with C# using the Application MyPhoneExplorer

Introduction

During my work in a Logistic Company we tried dusends of times to make an Application or an funktion in our ERP Application to easely send SMS. We tryed sending AT commands to phones or SIM modems, syncing with Outlook explorer and online Gateways but every of thoes solutions had something that made it not good for practical use. The common reason was that it didn´t work 100% like it should what means that the SMS message went trought the code/app/online servise but never arived to the recepient :( without any error message shown. Situations like this are in a Logistik Company a small Supergau. For serveral months our Disponent manager showd my an application that he found called MyPhoneExploere. More info about that are here. He told me that the dispoets are using now that app and asked me if I could integrate it in our ERP Applicaion for Order management. At the beggining I toght "no way" - It´s an external application, i don´t thing they made an API for external contol BUT after fuve minutes on Google and a simple question there it was. A way to control the functions of the app from external calls :) In couple of minutes the code was writen and several minutes later integrated in our ERP. Now after this solution worked for us several months with an 100% eficiense (how the disponemts explained to me :) ) I want to share it woth the comunity.

Beside that the Application MyPhoneExplorer is realy a genious small app that is totaly free and easy to use. I´m not trying to make marketing or something. But for is it works likw a sharm :D

The Code

Everithing that is needet to ake it work is that the Application MyPhoneExplorer is instaled, that it is runing and that a phone is connected with it. If we use Bluetooth the phone will be automaticaly connected if we are near the PC ;) as I sead - a great little app :D

The code we use to start the actions we store in a small static class called MyPhoneExplorer. The class has this 2 Properties that are crutial for the functionality:

  public static string MyPhoneExplorerApplicationName
        {
            get
            {
                return "MyPhoneExplorer";
            }
        }

        public static string MyPhoneExplorerFullPath
        {
            get
            {
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    if (clsProcess.ProcessName.Contains(MyPhoneExplorerApplicationName))
                    {
                        return clsProcess.Modules[0].FileName;
                    }
                }
                MessageBox.Show("MyPhoneExplorer ist nicht gestarted!", "MyPhoneExplorer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return string.Empty;

            }
        } 

The first one is just a static string. It could also be a const but who cares ;)

The second one is a string that returns us the ful path of the MyPhoneExploere application. The greate thing is that we don´t care "where" it is instaled because we get the path from the MyPhoneExplorer proccess. If the application is not runing we inform the user about that. If the application is not installed it´s the problem of the user if he tryes to start an application that is not installed ;) Of course this could be expanded that the message shows a link where to download the app or a link to a help how to use the app.

The next part of our class are 3 methods:

   public static bool SendSMS(string phoneNumber, string messageText)
        {
            string fileName = MyPhoneExplorerFullPath;
            if (fileName != string.Empty)
            {
                string arguments = String.Format(@" action=sendmessage savetosent=1 number={0} text=""{1}""", phoneNumber, messageText.Replace('"', '*'));
                StartPhoneExplorerWithArguments(fileName, arguments);
                return true;
            }
            return false;
        }

        public static void CallPhoneNumber(string phoneNumber)
        {
            string fileName = MyPhoneExplorerFullPath;
            if (fileName != string.Empty)
            {
                string arguments = String.Format(@" action=dial number={0}", phoneNumber);

                StartPhoneExplorerWithArguments(fileName, arguments);
            }

        }

        private static void StartPhoneExplorerWithArguments(string fileName, string arguments)
        {
            Process MyPhoneExplorer = new Process();
            MyPhoneExplorer.StartInfo.FileName = fileName;
            MyPhoneExplorer.StartInfo.Arguments = arguments;
            MyPhoneExplorer.Start();
        }
           

The last one is used to call the MyPhoneApplication with some arguments. We have it just to stay DRY.

The first method is used to send SMS messages. It recieves a phone number and the text message. The greate magic in this class is that we just get our Path of the MyPhoneExplorer application and generate the arguments string and at then call the last method.

EDIT: In the argument generation the massageText has a Replace that replaces the " chars to an *. The reason for that is that the " char interupts the argument parsing to the application causing that only the part of the message to " will be send and everithing after it will be just lost.

EDIT: The first method is turned to a bool. It returns true if the SMS was send and false if not. This way we can use it like this: (a code snipped from our ERP)

            if (MyPhoneExplorer.SendSMS(txtPhoneNumber.Text, txtMessageText.Text))
            {
                SMSSendMessages tblSendMessages = new SMSSendMessages(this, null);
                tblSendMessages.SaveSendMessage(User.ID, (int)cboDriverPhoneID.SelectedValue, txtPhoneNumber.Text, txtMessageText.Text);
                Close();
            } 

The second method does just the same thing generating only a different argument string.

And that is all it takes to work with it.

The Class MyPhoneExplorer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows.Forms;

namespace ICS.EnterpriseResourcePlaning
{
    public static class MyPhoneExplorer
    {

        public static string MyPhoneExplorerApplicationName
        {
            get
            {
                return "MyPhoneExplorer";
            }
        }

        public static string MyPhoneExplorerFullPath
        {
            get
            {
                foreach (Process clsProcess in Process.GetProcesses())
                {
                    if (clsProcess.ProcessName.Contains(MyPhoneExplorerApplicationName))
                    {
                        return clsProcess.Modules[0].FileName;
                    }
                }
                MessageBox.Show("MyPhoneExplorer ist nicht offen!", "MyPhoneExplorer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return string.Empty;

            }
        }

        public static bool SendSMS(string phoneNumber, string messageText)
        {
            string fileName = MyPhoneExplorerFullPath;
            if (fileName != string.Empty)
            {
                string arguments = String.Format(@" action=sendmessage savetosent=1 number={0} text=""{1}""", phoneNumber, messageText.Replace('"', '*'));
                StartPhoneExplorerWithArguments(fileName, arguments);
                return true;
            }
            return false;
        }

        public static void CallPhoneNumber(string phoneNumber)
        {
            string fileName = MyPhoneExplorerFullPath;
            if (fileName != string.Empty)
            {
                string arguments = String.Format(@" action=dial number={0}", phoneNumber);

                StartPhoneExplorerWithArguments(fileName, arguments);
            }

        }

        private static void StartPhoneExplorerWithArguments(string fileName, string arguments)
        {
            Process MyPhoneExplorer = new Process();
            MyPhoneExplorer.StartInfo.FileName = fileName;
            MyPhoneExplorer.StartInfo.Arguments = arguments;
            MyPhoneExplorer.Start();
        }

    }
}

Using the code

To send a SMS message we just use this code:

MyPhoneExplorer.SendSMS(txtPhoneNumber.Text, txtMessageText.Text); 

In this example the phone number is stored in a TextBox txtPhoneNumber and the message in the txtMessageText TextBox.

To dial a phone number we use this code:

MyPhoneExplorer.CallPhoneNumber(txtPhoneNumber.Text);  

Interesting is that if we make a call an dialog window is shown on the desktop where we can control the call.

Points of Interest

Besides this two actions there could be more done. We use only this two but if someone wants more it surrely could be done.

History

02.05.2014 - version 1.

02.05.2014 - version 2. txtMessage replacement added

02.05.2014 - version 3. the SendSMS method is turned to an bool

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here