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.

2 thoughts on “EmailMessage class Approach for Windows

  1. sibbl says:

    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.

    Reply
    1. Lance says:

      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.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.