#WPdev Quick tip – API for sending emails from your app

When developing Windows Phone or Windows Store app it’s often useful to give your user option to send you feedback email or feature request. In this short #WPdev article I’ll cover all three different approaches you could use in all current Windows Store platforms.

Windows Phone 7, 7.5, 8 and 8.1 (Silverlight)

Sending emails from original Windows Phone 7+ Silverlight apps is done using the EmailComposeTask:

string to = "my@email.cz";
string subject = "Idea for your app";
string body = "Hi, here's an idea for your cool app...";
EmailComposeTask emailTask = new EmailComposeTask
{
 To = to, Subject = subject, Body = body,
 //Cc = , Bcc = , CodePage = ,
};
emailTask.Show();

When the Show method is called the system dialog appears where you can choose the mail account that will be used for sending the email.

As you can see the usage is really simple, one class and one method is all you need. Note there is no way how to send emails with attachments using this API.
Another problem also is that this API is no longer available when developing Windows 8/8.1 or Windows Phone 8.1 WinRT apps. So what is the alternative?

Windows 8 and 8.1

In Windows 8.1 apps there is no simple API for composing and sending emails, but you can use one workaround – the mailto: Uri scheme:

string to = "my@email.cz";
string subject = "Idea for your app";
string body = "Hi, here's an idea for your cool app...";
string link = string.Format("mailto:{0}?subject={1}&body={2}", to, subject, Uri.EscapeDataString(body));
await Launcher.LaunchUriAsync(new Uri(link));

This launches the default system app that has registered the “mailto” Uri scheme as the default app. This is usually either Microsoft Outlook, or the Mail app, or it can be no app at all. Since the target app can be either Store app or classic Desktop app, the behavior might be a little strange, but it’s the best we have right now. Also be careful with the character encoding – since the email body is passed as a part of the query string. When I ran into an issue with badly formatted special characters, the Uri.EscapeDataString() method fixed it for me.

Note, again there is no way how to send emails with attachments using this API.

Windows Phone 8.1 (WinRT)

For launching email compose on new Windows Phone 8.1 platform you cannot use the EmailComposeTask from WP8. You can use the mailto Uri scheme though, but the user experience is not great. The best solution on this new platform is to use the Windows.ApplicationModel.Email.EmailMessage API:

string to = "my@email.cz";
string subject = "Idea for your app";
string body = "Hi, here's an idea for your cool app...";
EmailMessage mail = new EmailMessage();
mail.To.Add(new EmailRecipient(to));
mail.Subject = subject;
mail.Body = body;
//mail.CC = , mail.Bcc = , mail.Attachments =
await EmailManager.ShowComposeNewEmailAsync(mail);

Simple and straightforward, but with more power than on WP8 or Windows 8.1 – you can simply specify multiple addressee, both in To, Cc and Bcc fields. You can also specify both email address and name for each addressee. You can also finally add attachments to emails by specifying attachment name and providing Stream with file data.

When this API is used on Windows Phone 8.1, it launches just like EmailComposeTask system dialog for selecting target mailbox for sending the email.

The future in Windows 10

As you can see, it’s not easy to “just send emails” in Windows Phone and Windows tablet apps. Luckily it’s expected that there should be only one API for sending emails in upcoming Windows 10, most likely the last one, but if you are writing apps right now, the best you can do is place the email sending logic into Shared Code project and use conditional compilation and you are good to go.

#if WINDOWS_APP
// mailto Uri scheme
#elif WINDOWS_PHONE_APP
// EmailMessage API
#endif

If there is another native Email Compose API I may have missed, please, let me know in comments, thanks! 🙂

1 thought on “#WPdev Quick tip – API for sending emails from your app

  1. Pingback: #WPdev Quick tip – API for accessing Windows Store from your app « Martin Suchan – BloQ

Comments are closed.