WP7 - Send SMS in Window Phone 7

WP7 - Send SMS in Window Phone 7

In this article i will explain you how to send SMS in window phone 7.Sending a SMS is a feature of the Windows Phone operating system and can be used by the Launcher API. it uses the SmsComposeTask Launcher to open the native device SMS editor and give the user an option to send the SMS or discard it.


Step 1
To Develop application for Windows Phone 7 devices, you need to install Windows Phone 7.1 SDK.You can download latest SDK for Windows Phone
http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=27570

SDK 7.1.1 Update
https://dev.windowsphone.com/en-us/downloadsdk


Step 2
Create a Window Phone Application and give the solution name asSendSMSinWP7.
To start creating a new Windows Phone application, start Microsoft Visual Studio then create a new Project and select Windows Phone Application Template,it is look like this



Click on Image for better View

Step 3
Select the Window Phone Platform,it is look like this



Click on Image for better View

Step 4
Now design page of sending SMS,it is look like this
  1. <Grid x:Name="LayoutRoot" Background="Transparent">  
  2.         <Grid.RowDefinitions>  
  3.             <RowDefinition Height="Auto"/>  
  4.             <RowDefinition Height="*"/>  
  5.         </Grid.RowDefinitions>  
  6.   
  7.         <!--TitlePanel contains the name of the application and page title-->  
  8.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  9.             <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>  
  10.             <TextBlock x:Name="PageTitle" Text="Send SMS" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  11.         </StackPanel>  
  12.   
  13.         <!--ContentPanel - place additional content here-->  
  14.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  15.          <Grid.RowDefinitions>  
  16.           <RowDefinition Height="0.072*"/>  
  17.           <RowDefinition Height="0.124*"/>  
  18.     <RowDefinition Height="0.066*"/>  
  19.           <RowDefinition Height="0.61*"/>  
  20.           <RowDefinition Height="0.129*"/>  
  21.          </Grid.RowDefinitions>  
  22.      
  23.    <TextBlock Grid.Row="0" Grid.Column="0" Text="Enter Mobile No" FontSize="24"/>  
  24.    <TextBox x:Name="txtMobileNo" Grid.Row="1" Grid.Column="0"></TextBox>  
  25.    <TextBlock Grid.Row="2" Grid.Column="0" Text="Enter Message" FontSize="24"></TextBlock>  
  26.    <TextBox x:Name="txtMessage" Grid.Row="3" Grid.Column="0" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>  
  27.    <Button x:Name="btnSendSMS" Grid.Row="4" Grid.Column="0" Content="Send SMS" Click="btnSendSMS_Click"></Button>  
  28.      
  29.    </Grid>  
  30.     </Grid>  



Click on Image for better View

Step 5
On Button Send SMS click event,add the following Code,it is look like this
  1. private void btnSendSMS_Click(object sender, RoutedEventArgs e)  
  2.         {  
  3.             try  
  4.             {  
  5.                 //Create Instance of SmsComposeTask Launcher  
  6.                 SmsComposeTask smsComposeTask = new SmsComposeTask();  
  7.   
  8.                 // Specify Mobile phone number to whom the sms is to be sent  
  9.                 smsComposeTask.To = txtMobileNo.Text.Trim();  
  10.   
  11.                 // Body of Message  
  12.                 smsComposeTask.Body = txtMessage.Text.Trim();  
  13.   
  14.                 //Invoke the native sms edtior  
  15.                 smsComposeTask.Show();   
  16.             }  
  17.             catch (Exception ex)  
  18.             {  
  19.                 MessageBox.Show(ex.Message);   
  20.             }  
  21.   
  22.         }  

Run the Project.

Output


Click on Image for better View

When we click on send SMS button, it will open native SMS application.



Click on Image for better View
In here we can either edit the message or set other recipients.
Once the user clicks on Send Button on native SMS application, SMS will be sent.



Click on Image for better View

Full Code of XAML
  1. <phone:PhoneApplicationPage   
  2.     x:Class="SendSMSinWP7.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9.     mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"  
  10.     FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11.     FontSize="{StaticResource PhoneFontSizeNormal}"  
  12.     Foreground="{StaticResource PhoneForegroundBrush}"  
  13.     SupportedOrientations="Portrait" Orientation="Portrait"  
  14.     shell:SystemTray.IsVisible="True">  
  15.   
  16.     <!--LayoutRoot is the root grid where all page content is placed-->  
  17.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  18.         <Grid.RowDefinitions>  
  19.             <RowDefinition Height="Auto"/>  
  20.             <RowDefinition Height="*"/>  
  21.         </Grid.RowDefinitions>  
  22.   
  23.         <!--TitlePanel contains the name of the application and page title-->  
  24.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  25.             <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>  
  26.             <TextBlock x:Name="PageTitle" Text="Send SMS" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  27.         </StackPanel>  
  28.   
  29.         <!--ContentPanel - place additional content here-->  
  30.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  31.          <Grid.RowDefinitions>  
  32.           <RowDefinition Height="0.072*"/>  
  33.           <RowDefinition Height="0.124*"/>  
  34.     <RowDefinition Height="0.066*"/>  
  35.           <RowDefinition Height="0.61*"/>  
  36.           <RowDefinition Height="0.129*"/>  
  37.          </Grid.RowDefinitions>  
  38.      
  39.    <TextBlock Grid.Row="0" Grid.Column="0" Text="Enter Mobile No" FontSize="24"/>  
  40.    <TextBox x:Name="txtMobileNo" Grid.Row="1" Grid.Column="0"></TextBox>  
  41.    <TextBlock Grid.Row="2" Grid.Column="0" Text="Enter Message" FontSize="24"></TextBlock>  
  42.    <TextBox x:Name="txtMessage" Grid.Row="3" Grid.Column="0" TextWrapping="Wrap" AcceptsReturn="True"></TextBox>  
  43.    <Button x:Name="btnSendSMS" Grid.Row="4" Grid.Column="0" Content="Send SMS" Click="btnSendSMS_Click"></Button>  
  44.      
  45.    </Grid>  
  46.     </Grid>  
  47.    
  48.     <!--Sample code showing usage of ApplicationBar-->  
  49.     <!--<phone:PhoneApplicationPage.ApplicationBar>  
  50.         <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">  
  51.             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>  
  52.             <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>  
  53.             <shell:ApplicationBar.MenuItems>  
  54.                 <shell:ApplicationBarMenuItem Text="MenuItem 1"/>  
  55.                 <shell:ApplicationBarMenuItem Text="MenuItem 2"/>  
  56.             </shell:ApplicationBar.MenuItems>  
  57.         </shell:ApplicationBar>  
  58.     </phone:PhoneApplicationPage.ApplicationBar>-->  
  59.   
  60. </phone:PhoneApplicationPage>  


Download
Download Source Code

Comments