是否可以在Windows 8.1和Windows Phone 8.1的Windows通用应用程序中发送电子邮件?
await Launcher.LaunchUriAsync(new Uri("mailto:abc@abc.com?subject=MySubject&body=MyContent"));
有了这个代码行我可以发送一封电子邮件,但我想发送附件的消息。
由于Microsoft错过了将EmailMessage和EmailManager添加到Windows Store应用程序库,似乎只有两个不满意的解决方案:您可以使用共享或通过mailto协议发起电子邮件发送。 我是这样做的:
/// <summary> /// Initiates sending an e-mail over the default e-mail application by /// opening a mailto URL with the given data. /// </summary> public static async void SendEmailOverMailTo(string recipient, string cc, string bcc, string subject, string body) { if (String.IsNullOrEmpty(recipient)) { throw new ArgumentException("recipient must not be null or emtpy"); } if (String.IsNullOrEmpty(subject)) { throw new ArgumentException("subject must not be null or emtpy"); } if (String.IsNullOrEmpty(body)) { throw new ArgumentException("body must not be null or emtpy"); } // Encode subject and body of the email so that it at least largely // corresponds to the mailto protocol (that expects a percent encoding // for certain special characters) string encodedSubject = WebUtility.UrlEncode(subject).Replace("+", " "); string encodedBody = WebUtility.UrlEncode(body).Replace("+", " "); // Create a mailto URI Uri mailtoUri = new Uri("mailto:" + recipient + "?subject=" + encodedSubject + (String.IsNullOrEmpty(cc) == false ? "&cc=" + cc : null) + (String.IsNullOrEmpty(bcc) == false ? "&bcc=" + bcc : null) + "&body=" + encodedBody); // Execute the default application for the mailto protocol await Launcher.LaunchUriAsync(mailtoUri); }
你可以使用下面的附件发送邮件:
var email = new EmailMessage(); email.To = ...; email.Body = ...; email.Attachments.Add( ... ); var ignore = EmailManager.ShowComposeNewEmailAsync(email);
在Windows 8.1上,不幸的是,没有办法发送附件的电子邮件。 mailto协议是你所有的,它并不正式支持附件。 但是,您可以添加附件如下:
的mailto:?xxx@xxx.com受试者= XXX&体= XXX&ATTACH = C:\路径\到\文件
要么
的mailto:?xxx@xxx.com受试者= XXX&体= XXX&附件= C:\路径\到\文件
但由客户决定是否处理附件。 有关更多详细信息,请参阅此主题https://msdn.microsoft.com/zh-cn/library/aa767737(v=vs.85).aspx
发送带附件的电子邮件您将需要使用EmailMessage
和EmailManager
类。
1. EmailMessage:
EmailMessage类定义了将被发送的实际电子邮件。 您可以指定收件人(收件人,抄送,BC),主题和电子邮件正文。
2. EmailManager:
EmailManager类是在Windows.ApplicationModel.Email命名空间中定义的。 EmailManager类提供了一个静态方法ShowComposeNewEmailAsync,它接受EmailMessage作为参数。 ShowComposeNewEmailAsync将使用EmailMessage启动Compose电子邮件屏幕,允许用户发送电子邮件。
你可以找到更多的参考在这里Windows电话8-1和Windows运行时应用程序如何2发送电子邮件与附件在WP – 8 – 1
本页内容: https : //msdn.microsoft.com/en-us/library/windows/apps/xaml/hh871373.aspx
Microsoft提供了一个如何与应用程序共享的示例。 下载源示例应用程序: http : //go.microsoft.com/fwlink/p/?Linkid = 231511
打开解决方案并添加一个文件:test.txt到项目根目录。
然后打开ShareFiles.xaml.cs并用下面的代码替换类:
public sealed partial class ShareText : SDKTemplate.Common.SharePage { public ShareText() { this.InitializeComponent(); LoadList(); } List<Windows.Storage.IStorageItem> list { get; set; } public async void LoadList() { var uri = new Uri("ms-appx:///test.txt"); var item = await StorageFile.GetFileFromApplicationUriAsync(uri); list = new List<IStorageItem>(); list.Add(item); } protected override bool GetShareContent(DataRequest request) { bool succeeded = false; string dataPackageText = TextToShare.Text; if (!String.IsNullOrEmpty(dataPackageText)) { DataPackage requestData = request.Data; requestData.Properties.Title = TitleInputBox.Text; requestData.Properties.Description = DescriptionInputBox.Text; // The description is optional. requestData.Properties.ContentSourceApplicationLink = ApplicationLink; requestData.SetText(dataPackageText); requestData.SetStorageItems(list); succeeded = true; } else { request.FailWithDisplayText("Enter the text you would like to share and try again."); } return succeeded; } }
可能不是最好的代码,但它帮助我:)