Namespaces
Variants
Actions

How to send an SMS in Windows Phone 7

Jump to: navigation, search

This article explains how to send an SMS in a Windows Phone 7 application.

Article Metadata

Code Example
Source file: Media:SmsSender.zip

Tested with
SDK: Windows Phone SDK 7.1
Devices(s): Windows Phone emulator

Compatibility
Platform(s): Windows Phone

Article
Keywords: SmsComposeTask
Created: vineet.jain (20 Jan 2012)
Last edited: bintk (13 Apr 2013)

Introduction

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.

Note.png
Note: In contrast to Symbian, Windows Phone does not allow you to send sms "directly" or "silently" from your own app. As soon as sms sending initiation code below is executed the native sms editor app will be launched, giving the user the option to send the message or not.

Source code

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" />
</Grid>

With above code written in XAML, the page created will look like:

SmsMain.jpg

In the MainPage.xaml.cs, add the following code:

...
using Microsoft.Phone.Tasks;
 
namespace SmsSender
{
...
 
// function which gets called when 'Send SMS' button is clicked
string ismsboby;
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.

Sendsms.jpg

Once the user clicks on "Send" Button on native SMS application, SMS will be sent.

Sentsms (2).jpg

This page was last modified on 13 April 2013, at 22:24.
579 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved