Send Text Message to Cell Phones from a C# Application


This article describes a simple way to send text messages to a cellular phone from within a C# desktop application.  The source code provided includes a relatively good list of carriers to simplify the task of connecting with a cell phone and the task itself is really no more difficult than sending an email message through a desktop or web based application.

Getting Started

In order to begin, unzip the downloaded files and open the project provided.  Within the project you will find one main class:  frmMain.cs.  The main form is a windows application form and it contains a few controls necessary to capture the fields needed to properly form the message. These fields include:

  • Recipient's Phone Number:  Captures the recipient's cellular telephone number (10 digit)
  • Recipient's Carrier:  Captures the recipient's carrier.
  • Sender's email address:  Captures the sender's email address.
  • Sender's email server:  Captures the name of the sender's email server 
  • Message Subject Line:  Captures the message's title or subject
  • Message Body:  Captures the sender's message content.

The application is simple but could easily be improved by validating each of the required fields through the use of regular expressions or by at least validating that the text associated with each of the text boxes is not an empty string.  To maintain the simplicity of the project, little in the way of error handling has been included.

The following figure (Figure 1) shows a properly configured collection of input fields in use:

Figure 1: The Demonstration Application in Use

A quick review of the code will reveal that there is little going on there.  The following imports were added to the top of the class:

  1. using System;  
  2. using System.Net.Mail;  
The System.Net.Mail import brings in the support necessary to transmit the messages generated using the application.  Following the imports and the class declaration, there is a declarations region identified and within that region is a collection of private member variables; these private member variables are created in order to supply each of the required elements of the message.
  1. #Region "Declarations"  
  2.   // message elements  
  3.    private string mMailServer;  
  4.    private string mTo;  
  5.    private string mFrom;  
  6.    private string mMsg;  
  7.    private string mSubject;  
  8. #End Region  
At this point, the only thing left to do in code is to  write the following  three methods:
  1. private void frmMain_Load(System.Object sender, System.EventArgs e)  
  2. {  
  3.     // set up the carriers list - this is a fair list, you may wish to  
  4.     // research the topic and add others, it took a while to generate this  
  5.     // list...  
  6.     cboCarrier.Items.Add("");  
  7.     cboCarrier.Items.Add("");  
  8.     cboCarrier.Items.Add("");  
  9.     cboCarrier.Items.Add("");  
  10.     cboCarrier.Items.Add("");  
  11.     cboCarrier.Items.Add("");  
  12.     cboCarrier.Items.Add("");  
  13.     cboCarrier.Items.Add("");  
  14.     cboCarrier.Items.Add("");  
  15.     cboCarrier.Items.Add("");  
  16.     cboCarrier.Items.Add("");  
  17.     cboCarrier.Items.Add("");  
  18.     cboCarrier.Items.Add("");  
  19.     cboCarrier.Items.Add("");  
  20.     cboCarrier.Items.Add("");  
  21.     cboCarrier.Items.Add("");  
  22.     cboCarrier.Items.Add("");  
  23.     cboCarrier.Items.Add("");  
  24.     cboCarrier.Items.Add("");  
  25.     cboCarrier.Items.Add("");  
  26.     cboCarrier.Items.Add("");  
  27.     cboCarrier.Items.Add("");  
  28.     cboCarrier.Items.Add("");  
  29.     cboCarrier.Items.Add("");  
  30.     cboCarrier.Items.Add("");  
  31.     cboCarrier.Items.Add("");  
  32.     cboCarrier.Items.Add("");  
  33.     cboCarrier.Items.Add("");  
  34.     cboCarrier.Items.Add("");  
  35.     cboCarrier.Items.Add("");  
  36.     cboCarrier.Items.Add("");  
  37.     cboCarrier.Items.Add("");  
  38.     cboCarrier.Items.Add("");  
  39.     cboCarrier.Items.Add("");  
  40.     cboCarrier.Items.Add("");  
  41.     cboCarrier.Items.Add("");  
  42.     cboCarrier.Items.Add("");  
  43.     cboCarrier.Items.Add("");  
  44.     cboCarrier.Items.Add("");  
  45.     cboCarrier.Items.Add("");  
  46.     cboCarrier.Items.Add("");  
  47.     cboCarrier.Items.Add("");  
  48.     cboCarrier.Items.Add("");  
  49.     cboCarrier.Items.Add("");  
  50.     cboCarrier.Items.Add("");  
  51.     cboCarrier.Items.Add("");  
  52.     cboCarrier.Items.Add("");  
  53.     cboCarrier.Items.Add("");  
  54.     cboCarrier.Items.Add("");  
  55.     cboCarrier.Items.Add("");  
  56.     cboCarrier.Items.Add("");  
  57.     cboCarrier.Items.Add("");  
  58.     cboCarrier.Items.Add("");  
  59.     cboCarrier.Items.Add("");  
  60.     cboCarrier.Items.Add("");  
  61.     cboCarrier.Items.Add("");  
  62.     cboCarrier.Items.Add("");  
  63. }  
  64. private void btnSend_Click(System.Object sender, System.EventArgs e)  
  65. {  
  66.     // Collect user input from the form and stow content into  
  67.     // the objects member variables  
  68.     mTo = Trim(txtPhoneNumber.Text) & Trim(cboCarrier.SelectedItem.ToString());  
  69.     mFrom = Trim(txtSender.Text);  
  70.     mSubject = Trim(txtSubject.Text);  
  71.     mMailServer = Trim(txtMailServer.Text);  
  72.     mMsg = Trim(txtMessage.Text);  
  73.     // Within a try catch, format and send the message to  
  74.     // the recipient.  Catch and handle any errors.  
  75.     try  
  76.     {  
  77.         MailMessage message = new MailMessage(mFrom, mTo, mSubject, mMsg);  
  78.         SmtpClient mySmtpClient = new SmtpClient(mMailServer);  
  79.         mySmtpClient.UseDefaultCredentials = True;  
  80.         mySmtpClient.Send(message);  
  81.         MessageBox.Show("The mail message has been sent to " & message.To.ToString(), "Mail", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  82.     }  
  83.     catch (FormatException ex)  
  84.     {  
  85.         MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);  
  86.     }  
  87.     catch (SmtpException ex)  
  88.     {  
  89.         MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);  
  90.     }  
  91.     catch (Exception ex)  
  92.     {  
  93.         MessageBox.Show(ex.StackTrace, ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);  
  94.     }  
  95. }  
  96. private void btnExit_Click(System.Object sender, System.EventArgs e)  
  97. {  
  98.     // Upon user's request, close the application  
  99.     Application.Exit();  
  100. }  
At this point, the application should be complete you may wish to build the solution and test it.  Even though this example was intended to be simple, the overall concept may be used within an application to do some seemingly complex jobs.  For example, if you were tasked with writing an application that monitored some sort of trend information such as a daily stock price, and were to alert a group of end users whenever the stock price exceeded some predetermined, agreed upon value, you could do something such as looping through a collection of users subscribing to the stock price monitoring service and direct a text message to each of these users indicating that the watched stock had surpassed the threshold value. 

Also please note that, whilst it does cost you a dime to send a message to a cell phone in this manner, it may well cost the recipient something to receive it.  Bearing that in mind, as you test your version of the code, be mindful of expenses you may be generating for yourself (if, for example, you are sending messages to yourself) or another person.

Similar Articles