Click here to Skip to main content
Click here to Skip to main content
Go to top

How to Compose SMS in WP7 using the SmsComposeTask?

, 25 Apr 2012
Rate this:
Please Sign up or sign in to vote.
SmsComposeTask of the WP7 SDK can be used to create SMS from code.

In my 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 let's 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.

Reference: http://www.kunal-chowdhury.com. You may like to follow me on Twitter @kunal2383 or may like the Facebook page of my blog http://www.facebook.com/blog.kunal2383.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

_ Kunal Chowdhury _
Technical Lead
India India
Kunal Chowdhury is a Microsoft "Client Development" MVP (Most Valuable Professional), a Codeproject Mentor, Telerik MVP, Nokia Developer Champion, Speaker in various Microsoft events, Author, passionate Blogger and a Software Engineer by profession.
 
He is currently working in an MNC located in India. He has a very good skill over XAML, C#, Silverlight, Windows Phone, WPF and Windows 8 (WinRT). He posts his findings, articles in his technical blog and CodeProject.
 
Technical Blog: http://www.kunal-chowdhury.com
Facebook: http://facebook.com/blog.kunal
Twitter : http://twitter.com/kunal2383
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
Questionis there any option to send sms directly without user interaction? PinmemberLe@rner16-Dec-13 1:53 
AnswerRe: is there any option to send sms directly without user interaction? Pinmentor_ Kunal Chowdhury _16-Dec-13 3:22 
GeneralRe: is there any option to send sms directly without user interaction? PinmemberLe@rner19-Dec-13 19:44 
QuestionSMS PinmemberManov rao24-Apr-12 19:52 
AnswerRe: SMS Pinmvp_ Kunal Chowdhury _25-Apr-12 6:52 
SuggestionCode block formatting PinmvpMika Wendelius23-Apr-12 19:35 
AnswerRe: Code block formatting Pinmvp_ Kunal Chowdhury _24-Apr-12 6:38 
GeneralRe: Code block formatting PinmvpMika Wendelius24-Apr-12 6:42 
GeneralRe: Code block formatting Pinmvp_ Kunal Chowdhury _24-Apr-12 15:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 25 Apr 2012
Article Copyright 2012 by _ Kunal Chowdhury _
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid