Code to Send a SMS in Windows Phone 7


Launchers are used to perform tasks provided by the phone operating system. Sending a SMS is a feature of the Windows Phone operating system and can be used by the Launcher API. SmsCompseTask launcher is used to send SMS. This task class is defined as below,

SMSWP1.gif

To work with SmsComposeTask, first you need to add a namespace of Microsoft.Phone.Task

SMSWP2.gif

Then create an instance of SmsComposeTask.

SMSWP3.gif

Next you need to set required or optional parameter for the launcher task. Required parameters for SmsComposeTask are

  1. To number
  2. Body of SMS

So first set the parameter as below,

SMSWP4.gif

And SMS body as below,

SMSWP5.gif

Last step, you need to launch the SMS send application provided by operating system. You can show that as below,

SMSWP6.gif

If you put all the code together and want to send a SMS on the click event of button then the full code will be as below,

using System;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace test
{
    public partial class MainPage : PhoneApplicationPage
    {
               
        public MainPage()
        {
            InitializeComponent();

        } 

private void btnSendSMS_Click(object sender, RoutedEventArgs e)
        {           

            SmsComposeTask smsComposeTask = new SmsComposeTask();
            smsComposeTask.To = "9158153792";
            smsComposeTask.Body = "hey I am sending u SMS";          
            smsComposeTask.Show();          

        }
}
}


When you run the above code, you will get an application running as below,

SMSWP7.gif

When you click on Send SMS button, you will get a drafted SMS with TO and body set as below,

SMSWP8.gif

Now you can either edit the message or set other recipients or send SMS as it is. I hope this post was useful. Thanks for reading.

If you find my posts useful you may like to follow me on twitter http://twitter.com/debug_mode or may like Facebook page of my blog http://www.facebook.com/DebugMode.Net If you want to see post on a particular topic please do write on FB page or tweet me about that, I would love to help you.

Up Next
    Ebook Download
    View all
    Learn
    View all