【问题标题】:How to add an Attachment to Outlook Mail from UWP App programmatically?如何以编程方式从 UWP 应用程序向 Outlook 邮件添加附件?
【发布时间】:2017-04-09 17:55:03
【问题描述】:

我正在开发一个 UWP 应用程序,我想以编程方式从 UWP 应用程序向 Outlook 添加一个附件

如果有其他选择,请告诉我。

期待您的回复。

【问题讨论】:

    标签: outlook uwp windows-10 windows-10-universal uwp-xaml


    【解决方案1】:

    您可以使用共享合约将一些数据发送到合规应用程序(包括 Outlook)。它允许您与任何兼容的应用程序共享一些文本和数据。

    要激活共享,您只需注册 DataRequested 事件并显示共享 UI:

    DataTransferManager.GetForCurrentView().DataRequested += OnDataRequested;
    DataTransferManager.ShowShareUI();
    

    然后,在事件处理程序中:

    private async void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
    {
        var deferral    = args.Request.GetDeferral();
    
        try
        {   
            args.Request.Data.Properties.Title          = "Share Title"
            args.Request.Data.Properties.Description    = "Share some data/file";
            var file                                    = await ApplicationData.Current.TemporaryFolder.GetFileAsync("myFileToShare.xxx");
            args.Request.Data.SetStorageItems(new IStorageItem[] { logFile });
        }
        catch
        {
             args.Request.FailWithDisplayText("Unable to share data");
        }
        finally
        {
            deferral.Complete();
            sender.DataRequested    -= OnDataRequested;
        }
    }
    

    完成后,系统将显示共享 UI,用户可以在其中选择所需的应用程序。此应用将接收发送的数据。

    【讨论】:

      【解决方案2】:

      虽然@Vincent 的回答在您想使用共享合同时是完美的,但如果您想使用只是电子邮件并附加文件,下面是我在我的一个应用程序中使用的一个简单方法。

      internal async void ShowEmail(string body, string subject, StorageFile attachment)
      {
          EmailMessage email = new EmailMessage();
          email.Subject = subject;
          email.Body = body;
      
          var stream = RandomAccessStreamReference.CreateFromFile(attachment);
          email.SetBodyStream(EmailMessageBodyKind.Html, stream);
      
          await EmailManager.ShowComposeNewEmailAsync(email);
      }
      

      上面的方法是从Here中摘录的例子

      【讨论】:

      • 如果 (a) 您将 Outlook 作为默认电子邮件应用程序(或任何其他 Win32 应用程序)并且 (b) Outlook 之前未启动或 (c) 如果您尝试添加附件,则此方法将不起作用跨度>
      猜你喜欢
      • 1970-01-01
      • 2017-06-15
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 2010-10-22
      相关资源
      最近更新 更多