How to send an SMS in Windows Phone 7
This article explains how to send an SMS in a Windows Phone 7 application.
This code example shows how to send an SMS in a Windows Phone 7 app. The code example uses the SmsComposeTask (a type of Launcher) to open the native device SMS editor and give the user an option to send the SMS or discard it.
First create a sample windows phone application. This is done by choosing File->NewProject->Windows Phone Application in the Visual Studio 2010 . In the application a text label (label displaying the message to enter the SMS text), text editor (to enter the body for SMS) and a button (on the click of which sms editor opens up) are added in the MainPage.xaml file as follows:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBox Height="92" HorizontalAlignment="Left" Margin="56,121,0,0" Name="textBox1" Text="" VerticalAlignment="Top" Width="289" />
<TextBlock Height="52" HorizontalAlignment="Left" Margin="26,51,0,0" Name="textBlock1" Text="Please enter text for sms:" VerticalAlignment="Top" Width="256" />
<Button Content="Send SMS" Height="86" HorizontalAlignment="Left" Margin="96,276,0,0" Name="button1" VerticalAlignment="Top" Width="198" Click="button1_Click" />
With above code written in XAML, the page created will look like:
In the MainPage.xaml.cs, add the following code:
// function which gets called when 'Send SMS' button is clicked
private void button1_Click(object sender, RoutedEventArgs e)
ismsboby = textBox1.Text; // Fetch the text from textbox created in XAML
SmsComposeTask smsComposeTask = new SmsComposeTask();
smsComposeTask.To = "9012345566778"; // Mention here the phone number to whom the sms is to be sent
smsComposeTask.Body = ismsboby; // the string containing the sms body
smsComposeTask.Show(); // this will invoke the native sms edtior
As soon as 'Send SMS' button is clicked it opens up the native SMS application with body message & phone number filled which we passed from the code above.
Once the user clicks on "Send" Button on native SMS application, SMS will be sent.