Click here to Skip to main content
15,880,796 members
Articles / Mobile Apps / Windows Phone 7

How to Compose SMS in WP7 using the SmsComposeTask?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 Apr 2012CPOL2 min read 18.8K   2   9
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:

C#
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:

C#
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.

C#
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)


Written By
Technical Lead
India India

Kunal Chowdhury is a former Microsoft "Windows Platform Development" MVP (Most Valuable Professional, 2010 - 2018), a Codeproject Mentor, Speaker in various Microsoft events, Author, passionate Blogger and a Senior Technical Lead 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 app development. He posts his findings, articles, tutorials in his technical blog (www.kunal-chowdhury.com) and CodeProject.


Books authored:


Connect with Kunal on:





Comments and Discussions

 
Questionis there any option to send sms directly without user interaction? Pin
Le@rner16-Dec-13 1:53
Le@rner16-Dec-13 1:53 
AnswerRe: is there any option to send sms directly without user interaction? Pin
Kunal Chowdhury «IN»16-Dec-13 3:22
professionalKunal Chowdhury «IN»16-Dec-13 3:22 
GeneralRe: is there any option to send sms directly without user interaction? Pin
Le@rner19-Dec-13 19:44
Le@rner19-Dec-13 19:44 
QuestionSMS Pin
Manov rao24-Apr-12 19:52
Manov rao24-Apr-12 19:52 
AnswerRe: SMS Pin
Kunal Chowdhury «IN»25-Apr-12 6:52
professionalKunal Chowdhury «IN»25-Apr-12 6:52 
SuggestionCode block formatting Pin
Wendelius23-Apr-12 19:35
mentorWendelius23-Apr-12 19:35 
AnswerRe: Code block formatting Pin
Kunal Chowdhury «IN»24-Apr-12 6:38
professionalKunal Chowdhury «IN»24-Apr-12 6:38 
GeneralRe: Code block formatting Pin
Wendelius24-Apr-12 6:42
mentorWendelius24-Apr-12 6:42 
GeneralRe: Code block formatting Pin
Kunal Chowdhury «IN»24-Apr-12 15:44
professionalKunal Chowdhury «IN»24-Apr-12 15:44 

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

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