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:
XAML Code
- <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
- <Button x:Name="sendbtn" Content="Send" HorizontalAlignment="Left" Margin="172,477,0,0" VerticalAlignment="Top" Click="sendbtn_Click"/>
- <TextBox x:Name="txt_to" HorizontalAlignment="Left" Height="72" Margin="190,74,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="229"/>
- <TextBox x:Name="txt_body" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="229" Margin="190,305,0,0"/>
- <TextBox x:Name="txt_cc" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" Text=" " VerticalAlignment="Top" Width="229" Margin="190,228,0,0"/>
- <TextBox x:Name="txt_sub" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="229" Margin="190,151,0,0"/>
- <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="To :" VerticalAlignment="Top" Margin="117,99,0,0"/>
- <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Body :" VerticalAlignment="Top" Margin="93,330,0,0"/>
- <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Cc :" VerticalAlignment="Top" Margin="116,253,0,0"/>
- <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="Subject :" VerticalAlignment="Top" Margin="72,176,0,0"/>
- </Grid>
Once the design part is completed, go to the code behind page and add the following namespace to do the Email task:
- 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
- EmailComposeTask objsendemail = new EmailComposeTask();
- objsendemail.To = txt_to.ToString();
- objsendemail.Subject = txt_sub.ToString();
- objsendemail.Cc = txt_cc.ToString();
- objsendemail.Body = txt_body.ToString();
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.