12

I am developing a Windows Store Application (Windows 8).

I have a need to send emails based on data and address stored in the application data and without the need of the user to type it the data or the address.

What would be the right/easy way to implement it?

EitanB

David Anderson
  • 13,558
  • 5
  • 50
  • 76

5 Answers5

30

You can try with

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);
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
  • Use sharing for Windows 8 RT App – Manuel Rauber Oct 04 '12 at 19:57
  • Tried it and it works fine, but I think the "preferred" way would be through Share Charm. Thanks for your input! –  Oct 04 '12 at 23:08
  • 4
    The prefered way isn't always the share Charm. The share charm is used when you want to share specific content from your app. for example when i have a list of contacts in my app when i select a contact and press share i would assume i could share the contact details of this contact and not send him a mail. to send him a mail i would select the contact and press a mail icon in the app bar that uses mailto: and launches the mail app full screen. – GeertvdC Apr 17 '13 at 11:28
  • I tried it, but i can't see any new outlook window open. I am missing anything? – Shashank Bisen Apr 15 '14 at 07:34
  • Hi Yakoub, can you please tell me how to add attachment in my email, your above code works fine, but i need to add one image file as attachment from code. please help me.. – Rashad Valliyengal Jun 18 '14 at 07:57
  • The URL is wrong, the to field will never populate. You need to move the ? to just before subject and remove the to= – Greg Quinn Sep 03 '14 at 03:42
  • I wrote a class to [do that in a more friendly way](https://gist.github.com/sandrock/d607af1af95efe731ebd). It mimics the API available in universal apps. Beware of [this issue](http://stackoverflow.com/questions/22863013/unable-to-create-a-new-mail-with-multiple-recipients-with-mailto-uri) though. – SandRock Jul 08 '15 at 16:11
9

The correct way would be to use Sharing. Your app should create an HTML document or Text and share it. The user would select Mail from the Share charm and the HTML/Text would become the body of the email.

See here for more info...

http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh973055.aspx

Rob Durfee
  • 176
  • 1
  • The problem this with is (not with the special case mentioned in the question, though), is, when I e. g. have a feedback button, I can't redirect the user to the charm bar. – jalgames Jul 09 '14 at 20:03
6

This is the correct syntax to use for a mailto: link (unlike the other examples above with a mailto: which are incorrect..)

var mailto = new Uri("mailto:yourname@email.com?subject=" + subject + "&body=" + body);
await Launcher.LaunchUriAsync(mailto);

The problem with the mailto: method is if the user has no client program associated with mailto: nothing will happen.

The most reliable method to use is a web service or WCF service of some sort. Using the Share Charm while considered the 'correct' way on Windows 8, is not neccessarily the best as the user may still have no email client installed, for example if they rely on gmail.com for their email.

Greg Quinn
  • 1,927
  • 1
  • 23
  • 26
3

If you are developping a Universal WinRT Windows Phone application, you could use the "Windows.ApplicationModel.Email.EmailMessage" namespace as the "Microsoft.Phone.Tasks.EmailComposeTask" namespace doesn't work on WinRT application.

Then, uses this code to create and launch a new email.

// Create your new email message.
var em = new EmailMessage() ;

// Add as much EmailRecipient in it as you need using the following method.
em.To.Add(new EmailRecipient("yourname@yourdomain.com"));
em.Subject = "Your Subject...";
em.Body = "Your email body...";
// You can add an attachment that way.
//em.Attachments.Add(new EmailAttachment(...);

// Show the email composer.
await EmailManager.ShowComposeNewEmailAsync(em);

I hope it will solve your (or other developers) problem.

Regards.

Rem's
  • 442
  • 3
  • 8
1

It's always possible to connect to an SMTP server and issue commands like HELO, MAIL, RCPT, etc. Of course you'll need an SMTP server to connect to. I use this on our corporate intranet to send emails.

Vadim P
  • 43
  • 4