如何使用EWS托管API 2将ItemAttachments移动到文件夹

时间:2014-05-02 03:20:52

标签: c# email-attachments exchangewebservices ews-managed-api

我尝试编写一个简单的服务,将ItemAttachments移到任意Microsoft.Exchange.WebServices.Data.Folder。可以这样做吗?我使用的是Exchange 2010 SP2。

以下代码生成System.InvalidOperationException:" 附件不支持此操作。"

private void ProcessItem(ItemId id, Folder folder) 
{
    Item envelope = Item.Bind(_ExchangeService, id, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments));
    if (envelope.HasAttachments && envelope.Attachments[0] is ItemAttachment) 
    {
        ItemAttachment attachedItem = envelope.Attachments[0] as ItemAttachment;  // always only one
        attachedItem.Load(new PropertySet(ItemSchema.ItemClass));
        Item itemToMove = attachedItem.Item;
        try 
        {
            itemToMove.Move(folder.Id); //this is the bad boy!
        } 
        catch (Exception ex) 
        {
            Console.WriteLine(ex.ToString());
            throw;
        }
    }
}

非常感谢任何建议或帮助。

1 个答案:

答案 0 :(得分:1)

Glen Scales在Exchange TechNet Forum上回答了这个问题。

"由于Move,Copy只是真实商店物品的有效操作,因此无法正常工作。 ItemAttachment仍然是一个附件,因此您可以执行的唯一有效操作是与附件相关的操作。

EWS唯一真正的解决方法是,如果ItemAttachment是电子邮件消息,您可以获取ItemAttachment的MimeContent,然后将其上传到其他文件夹(这里你会失去一些保真度)或创建一个新项目并复制项目属性属性。否则,这是您应该使用MAPI执行的操作,即使这样您也需要将项目附件保存为MSG文件,然后将其上传到您想要的文件夹。"