Wensus Analytics

Windows 8 Metro app: Sending an email. (Compared to Windows Phone 7’s EmailComposeTask)

image

In a Windows Phone 7 app, sending an email requires an instance of EmailComposeTask. EmailComposeTask is really easy to use and self-explanatory:

EmailComposeTask emailComposeTask = new EmailComposeTask();

emailComposeTask.Subject = "message subject"; 
emailComposeTask.Body = "message body"; 
emailComposeTask.To = "recipient@example.com"; 
emailComposeTask.Show();

In Windows 8 Metro app, there’s no EmailComposeTask. Instead, you create a “mailto” –uri and launch it:

var mailto = new Uri("mailto:recipient@example.com"); 
await Windows.System.Launcher.LaunchUriAsync(mailto);

But when compared to an EmailComposeTask, this only fills the “To” –part, but what about the “Subject” and “Body”? Well, the mailto-syntax allows you to define these as query parameters. For example, here’s an example of defining the “subject”, “body” and “to” –fields from code. The uri is automatically escaped by the platform:

var mailto = new Uri("mailto:?to=recipient@example.com&subject=The subject of an email&body=Hello from a Windows 8 Metro app."); 
await Windows.System.Launcher.LaunchUriAsync(mailto);

image

With this knowledge, it would be rather easy to build a custom EmailComposeTask for Windows 8.

  • Cloud

    Thanks a lot!

  • lui

    Hey, a question. the tab and end of line chars are being removed from the email body. how do I make the MailFormat = to text?

    • http://mikaelkoskinen.net/ Mikael Koskinen

      Good question. I’m not actually sure how this should be handled. It’s been some time from your question so I wonder if you were already able to solve this?

  • http://www.facebook.com/kiril.stanoev Kiril Stanoev

    My email app is Outlook and when executing the suggested code, the To field is not prefilled with the desired address…

  • http://twitter.com/andreipop Andrei Pop

    Thanks for this!

  • Brandon

    Would like to see an example of a very custom e-mail, say one that includes debug info about the system as developers may like to implement in part of crash reporting. I am having trouble getting newlines to show up in the e-mail body either with rn or <br/>
    Thanks in advance!

  • YIN

    Hello , My question can be silly. I ‘m using default
    mail client . I want to close the mail app after send mail.

  • Tuğrul Emre Atalay

    Thanks for this unique topic!