Windows Phone 8 E-Mail Compose Task

Introduction

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

The Email Compose Task enables the application to launch an email application and options to send a mail message with cc, subject, bcc and so on.

Open the Visual Studio 2012 IDE and create a new Windows Phone 8 project with a valid name. Here I am using “EmailDemo”. Once the project is created, add some controls to do the email compose task. The design looks as in the following:

Email Demo in Windows Phone

XAML Code

  1. <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">  
  2.     <Button x:Name="sendbtn" Content="Send" HorizontalAlignment="Left" Margin="172,477,0,0" VerticalAlignment="Top" Click="sendbtn_Click"/>  
  3.     <TextBox x:Name="txt_to" HorizontalAlignment="Left" Height="72" Margin="190,74,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="229"/>  
  4.     <TextBox x:Name="txt_body" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="229"     Margin="190,305,0,0"/>  
  5.     <TextBox x:Name="txt_cc" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="229" Margin="190,228,0,0"/>  
  6.     <TextBox x:Name="txt_sub" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="229" Margin="190,151,0,0"/>  
  7.     <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="To :" VerticalAlignment="Top" Margin="117,99,0,0"/>  
  8.     <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Body :" VerticalAlignment="Top" Margin="93,330,0,0"/>  
  9.     <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Cc :" VerticalAlignment="Top" Margin="116,253,0,0"/>  
  10.     <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Subject :" VerticalAlignment="Top" Margin="72,176,0,0"/>  
  11. </Grid>  

Once the design part is completed, go to the code behind page and add the following namespace to do the Email task: 

  1. Using Microsoft.Phone.Tasks;  
Microsoft.Phone.Tasks.EmailComposeTask contains a set of properties to do the email compose task. The properties are: 
  • To
  • Cc
  • Bcc
  • Body
  • Subject
  • Codepage

Now go to the button click event and write the following code to do the email compose task.

First create a new object for the EmailComposeTask to send a new Email to any recipient.

C# Code

  1. EmailComposeTask objsendemail = new EmailComposeTask();  
  2. objsendemail.To = txt_to.ToString();  
  3. objsendemail.Subject = txt_sub.ToString();  
  4. objsendemail.Cc = txt_cc.ToString();  
  5. objsendemail.Body = txt_body.ToString();  

Code View

Now our code is completed. Just run the application and see the output, it looks like the following because we cannot test this task using the Windows Phone 8 emulator. You can test this application in your device.

View in Windows Phone

Up Next
    Ebook Download
    View all
    Learn
    View all