Click here to Skip to main content
15,887,596 members
Articles / Programming Languages / C#
Tip/Trick

Send SMS and make Calls with MyPhoneExplorer

Rate me:
Please Sign up or sign in to vote.
4.87/5 (11 votes)
2 May 2014CPOL4 min read 23.6K   16   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:

C++
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:

C++
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)

C++
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

C++
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:

C++
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:

C++
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer ICS Logistik & Transport GmbH
Germany Germany
Born in Bosnia and Herzegowina where I studied Traffic and Communication in the University of Sarajevo. After the Bachelor, found a Job in a Logistic Company in Germany where I live and work now as an Software developer for our Company needs.

With programming I started as an hoby at work. For now I have almost 2 years programing experience. First with excel then VBA in Excel. That growed up to VBA with Access and a first Access DB. Then an SQL Server camed in and VBA with Access could not handle it. The next move was of cource VB.Net but with Visual Studio I came in contact with C#.

Comments and Discussions

 
QuestionCaller id Pin
Member 148061349-May-20 9:24
Member 148061349-May-20 9:24 
QuestionThanks Pin
Lakshmi Reddy2-Nov-18 19:13
Lakshmi Reddy2-Nov-18 19:13 
SuggestionSpelling Pin
_Noctis_2-May-14 15:46
professional_Noctis_2-May-14 15:46 

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.