×
Namespaces

Variants
Actions

How to send an SMS in Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search

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

WP Metro Icon Messages.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code Example
Source file: Media:SmsSender.zip
Tested with
Devices(s): Windows Phone emulator
CompatibilityArticle
Created: vineet.jain (20 Jan 2012)
Last edited: hamishwillee (29 Jul 2013)

Introduction

This code example shows how to send an SMS in a Windows Phone 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.pngNote: 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 29 July 2013, at 07:37.
354 page views in the last 30 days.
×