我正在尝试向我的C#/ .Net应用程序添加一个function,以便用户通过电子邮件发送文件。 当用户安装Outlook时,我可以成功地使用Outlook互操作API来完成我想要的function。 但是,在新的Windows 10安装,我不能解决如何打开一个电子邮件与默认的邮件应用程序,这是从Windows应用程序的附件。
我努力了:
使用EML文件,按照https://stackoverflow.com/a/25586282/2102158
使用MAPI32.dll等(我使用的代码从https://github.com/metageek-llc/inSSIDer-2/blob/master/MetaScanner/UnhandledException/MapiMailMessage.cs )
使用mailto:链接。
也
Windows.ApplicationModel.Email.EmailMessage似乎只在手机上可用。
我不想用SMTP来发送消息服务器端。
我也尝试了与邮件应用程序关联的MS-UNISTORE_EMAIL:和OUTLOOKMAIL:urlscheme,它们似乎与mailto的行为相同:
似乎没有任何方法从命令行启动邮件应用程序
请尝试下面的例子
private async void SendEmailButton_Click(object sender, RoutedEventArgs e) { EmailMessage emailMessage = new EmailMessage(); emailMessage.To.Add(new EmailRecipient("***@***.com")); string messageBody = "Hello World"; emailMessage.Body = messageBody; StorageFolder MyFolder = Windows.ApplicationModel.Package.Current.InstalledLocation; StorageFile attachmentFile =await MyFolder.GetFileAsync("MyTestFile.txt"); if (attachmentFile != null) { var stream = Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(attachmentFile); var attachment = new Windows.ApplicationModel.Email.EmailAttachment( attachmentFile.Name, stream); emailMessage.Attachments.Add(attachment); } await EmailManager.ShowComposeNewEmailAsync(emailMessage); }
ShowComposeNewEmailAsny(...)部分是魔术部分。