kunal-chowdhury.com

How to Compose SMS in WP7 using the SmsComposeTask?


In last few articles I was discussing on Launchers and Choosers of Windows Phone 7. In today’s blog post I am going to continue on that again. This time we will discuss on “SmsComposeTask” of the WP7 SDK, which you can use to create SMS from code.

 

SmsComposeTask launches the Messaging application with a new SMS message displayed and requires user intervention to send the message. Continue reading to learn in depth of the API implementation and uses of the API.

 

Related Articles

 

Know About the API

SmsComposeTask” is a sealed class present in the “Microsoft.Phone.Tasks” namespace and contains two properties called “Body” and “To”. You can set the phone number in “To” field where you want to send the sms. Set the message in the “Body” field before you call the Show() method to launch the messaging application. Both the fields are optional. Once the application launched in the UI, user can set those manually in any case.

 

Here is the meta data of the SmsComposeTask:

 

 
namespace Microsoft.Phone.Tasks
{
    public sealed class SmsComposeTask
    {
        public string Body { get; set; }
        public string To { get; set; }
 
        public void Show();
    }
}

 

 

Do you want to know how the class has been implemented internally by the SDK to launch the messaging application? Here is the decompiled version of the SDK class which will help you to understand the API implementation:

 

 
namespace Microsoft.Phone.Tasks
{
  public sealed class SmsComposeTask
  {
    public string Body { get; set; }
    public string To { get; set; }
 
    public void Show()
    {
      if (!ChooserHelper.NavigationInProgressGuard((Action) (() => this.Show())))
        return;
      ParameterPropertyBag ppb = new ParameterPropertyBag();
      string to = this.To;
      if (!string.IsNullOrEmpty(to))
        ppb.CreateProperty("To").StringValue = to;
      string body = this.Body;
      if (!string.IsNullOrEmpty(body))
        ppb.CreateProperty("Body").StringValue = body;
      ppb.CreateProperty("Account").StringValue = "{FD39DA85-C18F-4c0c-AEAC-75867CEA7876}";
      ChooserHelper.Navigate(
                    new Uri("app://5B04B775-356B-4AA0-AAF8-6491FFEA5614/ShareContent",
                    UriKind.Absolute), ppb);
    }
  }
}

 

 

The SDK creates the ParameterPropertyBag to set all the properties before launching the APIs. Hope the above code snippet was helpful for you to understand the same.

 

Implementation Steps

Now lets see how to write a code to integrate the sms compose feature in your WP7 application. First create the instance of the class and set the optional properties as shown below and call the Show() method to launch the application.

 

 
var smsComposeTask = new SmsComposeTask
                         {
                             To = "+919998881523", 
                             Body = "Check out www.kunal-chowdhury.com for WP7 articles"
                         };
smsComposeTask.Show();

 

Remember that, there will be user intervention before sending the SMS and you can’t control sending the SMS silently. Let’s see how the application works after the Show() call.

 

Once you call the Show() method, the application launches in the screen (figure 1) and the user can change the phone number or the message from the application. Once user taps the “Send” button, the message will deliver to the number mentioned in the “To” field and then you will be able to write more message to the same number. If you tap outside, it will come out from the input screen as shown below:

 

Screenshot 1: How to Compose SMS in WP7 using the SmsComposeTask?  Screenshot 2: How to Compose SMS in WP7 using the SmsComposeTask?  Screenshot 3: How to Compose SMS in WP7 using the SmsComposeTask?

 

I hope that this post was very useful for you to understand the SDK API, it’s internal code implementation and the sample code implementation. Please leave your feedback below to leave your comment.

 

Stay tuned to my blog, twitter or facebook to read more articles, tutorials, news, tips & tricks on various technology fields. Also Subscribe to our Newsletter with your Email ID to keep you updated on latest posts. We will send newsletter to your registered email address. We will not share your email address to anybody as we respect privacy.


Kunal Chowdhury
If you have come this far, it means that you liked what you are reading (How to Compose SMS in WP7 using the SmsComposeTask?).

Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions.

Authored Books: