C# VSTO Outlook - select 多个项目并将它们作为附件发送

C# VSTO Outlook - select multiple items and send them as attachment

如何 select 多个项目并打开新邮件表单并将所有 selected 项目作为附件添加到新电子邮件,以便我可以将它们与新电子邮件一起发送?

我想,这可以分解为:

  1. 如何在 outlook 中获取多个 selected 项目(比如用户 select 按住 Ctrl 键执行多个 selection 时全部处理)?

  2. 如何在 Outlook 中打开新电子邮件表单?

  3. 如何在上面的 1 中附加 selected 项目并将它们作为附件添加到新电子邮件中?

更新: 到目前为止,我已经设法做到了以下几点(感谢 Dmitry 的评论):

public void SendSelectedMailsAsAttachment()
{
    try
    {
        Selection olSelection = HostAddIn.ActiveExplorer.Selection;
        var count = olSelection.Count;
        var items = new List<IItem>();

        Microsoft.Office.Interop.Outlook.MailItem oMailItem = HostAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
        oMailItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML;

        foreach (var sel in olSelection)
        {
            oMailItem.Attachments.Add(sel);
        }
        oMailItem.Display(false);
    }  
    catch (Exception ex)
    {
        //ex message
    }
}

但使用此代码,会发生以下情况:

  1. 如果我 select 单封电子邮件并尝试将其作为附件发送,上面的代码执行得很好并且新的电子邮件表单打开但它不会显示附件。

  2. 如果我 select 多封电子邮件并尝试将它们作为附件发送,上面的代码将在第二次调用 oMailItem.Attachments.Add(sel) 时抛出异常“This operation is not supported until the entire message is downloaded. Download the message and try again."

  1. Application.ActiveExplorer.Selection collection
  2. Application.CreateItem / MailItem.Display
  3. 遍历 Application.ActiveExplorer.Selection collection 中的项目并为每个项目调用 MailItem.Attachments.Add