How do I send an email from a WinRT/Windows Store application?

你离开我真会死。 提交于 2019-11-27 21:29:00

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

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);

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!