Windows Phone 8 SMS Compose Task

Introduction

In this article we will see how to send a SMS from Windows Phone 8.

The SMS Compose Task enables the application to launch a messaging application and options to send a SMS. We can assign the recipient by default or provide an option to the end user to select the recipient from the contacts.

Let us see the procedure to do a SMS task from the Windows Phone 8 application. Open the Visual Studio 2012 IDE and create a new Windows Phone 8 application with a valid name. Once the application is created add some controls to do the SMS compose task. The design looks as in the following:

SMS Demo in Windows Phone

We need to set the input scope as a number for “To TextBox”, because to enter a number only. Change the input scope of “To TextBox” as “Number” as in the following:

TextBox Properties in Windows Phone

XAML Code

  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.     <Button x:Name="btn_send" Content="Send SMS" HorizontalAlignment="Left" Margin="161,370,0,0" VerticalAlignment="Top" Click="btn_send_Click"/>  
  3.     <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Message:" VerticalAlignment="Top" Margin="66,229,0,0"/>  
  4.     <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="To:" VerticalAlignment="Top" Margin="64,128,0,0"/>  
  5.     <TextBox x:Name="txt_to" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="221" Margin="187,103,0,0" InputScope="Number"/>  
  6.     <TextBox x:Name="txt_body" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="221" Margin="187,203,0,0"/>   
  7. </Grid>  
Once the design part is completed, go to the code behind page and add the following namespace to do the SMS task.
  1. Using Microsoft.Phone.Tasks;  
Microsoft.Phone.Tasks.SMSComposeTask contains the following two properties to do the SMS compose task:

 

  • To
  • Body

Now go to the button click event and write the following code to do the SMS compose task. First create a new object for the SmsComposeTask to send a new SMS to any recipient.

C# Code

  1. private void btn_send_Click(object sender, RoutedEventArgs e)  
  2. {  
  3.     SmsComposeTask objSendsms = new SmsComposeTask();  
  4.     objSendsms.To = txt_to.Text.ToString();  
  5.     objSendsms.Body = txt_body.Text.ToString();  
  6.     objSendsms.Show();  
  7. }  
Now our code is completed. Just run the application and see the output. Enter the recipient number and message in the given TextBox and click “Send SMS” button.

You can test this application in your device. Finally your output looks as in the following:

 

Final SMS Demo in WP Sent Message in Windows Phone