EmailMessage class Approach for Windows

Unfortunately, there is no EmailMessage class for big Windows. It is such a nice feature of Windows Phone to be able to quickly compose an email programmatically and launch the built-in email app. However, I have created a working approach for the windows portions of my Universal Windows apps and I wanted to share it with you.

My first attempt was with using a ShareContract, but it doesn’t allow you to fill in the “To:” field and sometimes my error report data was too long to fit into DataTransferManager’s SetText() method.

So I decided to take a look at using a UriScheme to launch another app, I do this in many of my Windows Phone 8 apps to share data. I found a perfect solution for my needs with WinRT’s Launcher class. Here is the finished result:


private async Task<bool> ReportErrorMessage(string detailedErrorMessage)
{
var uri = new Uri(string.Format("mailto:email.address@outlook.com?subject=Error Report&body={0}", detailedErrorMessage), UriKind.Absolute);

var options = new Windows.System.LauncherOptions
{
DisplayApplicationPicker = true,
DesiredRemainingView = Windows.UI.ViewManagement.ViewSizePreference.UseLess,
PreferredApplicationPackageFamilyName = "microsoft.windowscommunicationsapps_8wekyb3d8bbwe",
PreferredApplicationDisplayName = "Mail"
};

return await Windows.System.Launcher.LaunchUriAsync(uri, options);
}

I needed this so that user can send me error reports when there is an unexpected crash, here’s an explanation of the code:

  1. I created a Task<bool> that accepts a string to pass the error message.
  2. I construct a “mailto” uri, which lets me prefill the email’s To, Subject and Body fields (notice that I am using the detailedErrorMessage string for the email body)
  3. The most interesting part with the Launcher is that you can set some great LauncherOptions like; “open with” app picker, screen size, preferred app, UI options, content type and more. I highly recommend that you take a look at the LaunchUriAsync documentation to see what’s available to fit your needs.
  4. Lastly, I made a return statement from LaunchUriAsync so I can let the caller know it was successful or not.

I hope this helps you prepare something until Windows 10 (hopefully) shares some of Windows Phone’s awesome built-in features like EmailManager.

Advertisements

2 thoughts on “EmailMessage class Approach for Windows

  1. Nice find, but shouldn’t apps rather respect the user’s email application of choice? As a user who uses Outlook instead of the default mail application, I would expect Outlook to be opened.

    • I agree, thats why I add the option for the “open with” dialog to popup. If your default is a new Outlook email on the desktop, it will use that instead.

If you have a comment, leave it below. If you have a question, email me directly.

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s