Sending SMS messages programatically

Screenshot of Send SMS applicationWhen developing line of business applications for field service agents it can be handy to have the application send SMS text messages to alert customers of updated status information, such as the potential for the service agent to arrive late for an appointment. This blog post discusses how to send SMS text messages programatically via the .NET Compact Framework.

Supported Platforms

This blog post makes use of classes within the Microsoft.WindowsMobile.PocketOutlook assembly. This assembly is not part of the .NET Compact Framework, instead it is shipped as part of the Windows Mobile operating system.

The assembly was first introduced as part of Windows Mobile 5.0. If you need to send SMS messages from a Windows Mobile 2003 device you will need to utilise a third party component such as the Mobile In The Hand product (which provides a compatible interface) or manually wrap the underlying native APIs.

In order for the demos in this blog post to work you need to add references to the following two assemblies:

If you forget the reference to the Microsoft.WindowsMobile assembly you will get the following compile time error:

The type ‘Microsoft.WindowsMobile.IApplicationLauncher’ is defined in an assembly that is not referenced. You must add a reference to assembly ‘Microsoft.WindowsMobile, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35′.

Creating an SMS Message

To create a new SMS text message you need to create an instance of the SmsMessage class and then set its various properties.

using Microsoft.WindowsMobile.PocketOutlook;
SmsMessage message = new SmsMessage();
message.To.Add(new Recipient("Jane Doe", "+1 45 123456"));
message.Body = "Would you like to go to lunch?";

The Body property is a string which contains the message you want to send. Notice that the To property is a collection of recipients, so a single SMS can be addressed to one or more recipients.

There is even a constructor overload which helps with the common case of a simple message intended for a single recipient:

SmsMessage message = new SmsMessage("+1 45 123456",
  "Would you like to go to lunch?");

Sending an SMS Message

Once we have created the SMS message we need a way to cause it to be sent. There are a couple of ways to achieve this.

The easiest is to call the send method on the SmsMessage instance as shown below:

// Send the SMS to its recipient(s)

Calling the send method sends the SMS message behind the scenes with no visual indication that something is occurring.

Alternatively if you would like to give the user a chance to review and edit the contents of the message before it is sent you can display the message within the built in messaging application via the MessagingApplication.DisplayComposeForm method.

// Display the new SMS in the standard
// messaging application

The third and final way is to create an instance of the OutlookSession class and use it’s SmsAccount property as follows:

using (OutlookSession session = new OutlookSession())

Testing within an Emulator

The Windows Mobile 6 SDK introduced a Cellular Emulator tool which makes it easy to test applications which interact with cellular phone based functionality. One advantage of using this tool to test SMS sending applications is that it avoids the charges typically associated with sending SMS messages via real devices.

You can find the Cellular Emulator within your desktop’s Windows start menu. The first step in using the Cellular Emulator is to connect it to your device emulator. This can be achieved by following the Cellular Emulator Quick Start instructions available on MSDN.

If you need further assistance configuring the Cellular Emulator, Jim Wilson has created a great video titled “How Do I: Configure the Device Emulator to Use an Emulated Cellular Connection?“.

Once correctly configured you can switch to the SMS tab of the Cellular Emulator to send messages to, or view messages received from the device emulator.

Screenshot of SMS tab within the Cellular Emulator application

Demo Application

[Download - 9KB]

A small example application is available for download which demonstrates how to send SMS messages programatically. It displays a simple form to capture the desired message and recipient phone number and then demonstrates a few techniques for sending the message.

Of note is a checkbox which makes the program append onto the end of the user’s message the current battery charge status. This is a lead into the next blog post which will discuss how to programmatically respond to a Windows Mobile device receiving a SMS text message.

5 Responses to “Sending SMS messages programatically”

  1. someguy198650 says:

    i’m having a problem saying “Could not load sms.dll”

  2. Jagannath says:

    Your Article is very useful for me. thanks!

  3. Andrew says:

    THANK YOU SO MUCH. Do you think I could find this MessagingApplication.DisplayComposeForm(message); on MSDN!!

  4. Rakhi says:

    I also facing same problem of loading sms.dll

  5. guillermo says:

    Simple and effective.

Leave a Reply