#1 How to perform email tasks in a WP7 app
published on: 10/29/2010 | Views: N/A | Tags: Tasks
This post is the first in a series of articles covering the WP7 Launcher and Choosers APIs. In my previous post I explained everything you need to know about the different phone tasks. Now it is time to focus on how to perform email manipulations with : SaveEmailAddressTask, EmailAddressChooserTask and EmailComposeTask .
It is a pretty common scenario for a phone app to :
- compose email
- send emails
- add new email address to the contact details
- select email address from the existing ones.
SaveEmailAdress to the contact details
This task can be acomplish by the SaveEmailAddressTask Class :
Namespace: Microsoft.Phone.Tasks
Assembly: Microsoft.Phone (in Microsoft.Phone.dll)
It allows your application to launch the Contacts application and enables users to save a new email address to the Contacts list. This is done by calling the Show() method of the SaveEmailAddressTask object. You can Obtain the result of the chooser operation by handling the Completed event.
Example:
SaveEmailAddressTask saveEmailAddressTask;
this.saveEmailAddressTask = new SaveEmailAddressTask();
this.saveEmailAddressTask.Completed += new EventHandler<TaskEventArgs>(saveEmailAddressTask_Completed);
private void btnSaveAddress_Click(object sender, RoutedEventArgs e)
{
saveEmailAddressTask.Email = "testmail@test.com";
saveEmailAddressTask.Show();
}
private void saveEmailAddressTask_Completed(object sender, TaskEventArgs e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show("Email successfully Saved..");
}
}
You can always manipulate the current SaveEmailAddressTask through its defaultfault properties/methods.
Choose EmailAdress from the existing ones
Choosing email address from the existing ones can be just so easily implemented through the EmailAddressChooserTask. It launches the phone Contacts application and allows the user to select particular email address.
Example:
EmailAddressChooserTask emailAddressChooserTask;
this.emailAddressChooserTask = new EmailAddressChooserTask();
this.emailAddressChooserTask.Completed += new EventHandler<EmailResult>(emailAddressChooserTask_Completed);
private void btnChoseEmailAddress_Click(object sender, RoutedEventArgs e)
{
emailAddressChooserTask.Show();
}
private void emailAddressChooserTask_Completed(object sender, EmailResult e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show("Selected email :" + e.Email);
}
}
Composing email
The third launcher (EmailComposeTask) allows you to send email from your application by launching the Email application which displays a new email message. It expects the mail address which could be provided just as a string, but in- real world application user expect to select it from his contacts and if not found enter manually.
Example 1:
private void emailAddressChooserTask_Completed(object sender, EmailResult e)
{
if (e.TaskResult == TaskResult.OK)
{
MessageBox.Show("Selected email :" + e.Email);
//in-real world application user expect to select it from his contacts and if not found enter manually.
//EmailComposeTask emailComposeTask = new EmailComposeTask();
//emailComposeTask.To = e.Email;
//emailComposeTask.To = saveEmailAddressTask.Email;
//emailComposeTask.Body = "WP7 Emails Demo";
//emailComposeTask.Cc = "testmail2@test.com";
//emailComposeTask.Subject = "Windows Phone 7";
//emailComposeTask.Show();
}
}
Example 2:
private void composeMail_Click(object sender, RoutedEventArgs e)
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.To = "chris@example.com";
emailComposeTask.To = saveEmailAddressTask.Email;
emailComposeTask.Body = "WP7 Emails Demo";
emailComposeTask.Cc = "testmail2@test.com";
emailComposeTask.Subject = "Windows Phone 7";
emailComposeTask.Show();
}
Note: Unfortunately some Launchers don't work in the emulator. For example, the EmailComposeTask assumes you have an email account set up on the device. Because the emulator prevents you from creating email accounts, you won't be able to test this.
That was all about performing email manipulations in a WP7 App. You can download the full source code of this article here.
In the next post I will talk about PhotoChooserTask and CameraCaptureTask.
You can also follow us on Twitter @winphonegeek
Comments
attachments
posted by: ramki on 8/24/2011 10:27:10 AM
I couldn't find API for attachments. Is there a way, other than using webservices?
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
