Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Playing with the Email Compose Task in Windows Phone

DZone's Guide to

Playing with the Email Compose Task in Windows Phone

Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

In this how-to tutorial we are going to see an important and useful task for composing an email using the Launchers that are available as part of Windows Phone. To get some clear idea on Launchers and for a list of Launchers check this article on Working with Launchers in WP7. The Email Compose Task allows the application to launch the email application and opts to enter a new mail message. We can play around with providing the different options like having the recipients name, Email id, subject, to, cc, bcc etc. Once we are done with the required options the email will be sent once the send button has been clicked, which will be available as part of the Email application. Let us see the different code on how to make use of the Email Compose Task in Windows Phone development.

Open Visual Studio 2010 IDE and create a new Silverlight for Windows Phone project with a valid project name as shown in the screen below. Once the project is created add some controls which are used to trigger the Email Compose task.

2012-03-27 07h25_23

Now let us make some changes to the UI, which on button clicks will launch the Email Message application. We are going to add a button which will trigger the email compose task. Once we added the button in the code behind we need to start with the code. The Email Compose task has the assembly Microsoft.Phone.Task as the namespace, which is needed to be added in the using statement as below code

using Microsoft.Phone.Tasks;

Microsoft.Phone.Task.EmailComposeTask has the below set of properties which are used to play around with the email message sending application which basically creates a new Email Message and sends it to different recipients.

Properties:



Now in the button click event we need to play around with the code as shown below.

Code:

using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace F5debugWp7EmailComposeTask
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            EmailComposeTask f5EmailCompose = new EmailComposeTask();
           
            f5EmailCompose.To = “Karthikeyan@f5debug.net”;
            f5EmailCompose.Subject = “F5Debug Tutorial”;
            f5EmailCompose.Body = “EMail Compose Task Sample Application”;
            f5EmailCompose.Show();

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            EmailComposeTask f5EmailCompose = new EmailComposeTask();

            f5EmailCompose.To = “Karthikeyan@f5debug.net”;
            f5EmailCompose.Subject = “F5Debug Tutorial”;
            f5EmailCompose.Body = “EMail Compose Task Sample Application”;
            f5EmailCompose.Cc = “KarthikCC@f5debug.net”;
            f5EmailCompose.Show();

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            EmailComposeTask f5EmailCompose = new EmailComposeTask();

            f5EmailCompose.To = “Karthikeyan@f5debug.net”;
            f5EmailCompose.Subject = “F5Debug Tutorial”;
            f5EmailCompose.Body = “EMail Compose Task Sample Application”;
            f5EmailCompose.Bcc = “KarthikBCC@f5debug.net”;
            f5EmailCompose.Show();

        }
    }
}

Now we are done with our code, just run the application and we can see the expected output as shown below. Basically we will not have the option to test with the emulator as the emulator will prompt you to set the internet to send the message. So we can set up the account and then try to use the code which will send the mail accordingly.

Output:

image

 

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Karthikeyan Anbarasan, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}