7

可以保存一个ItemAttachment吗?对于FileAttachment我们使用以下 EWS 托管 API 代码进行保存,

   if(attachment is FileAttachment)
    {
      FileAttachment fAttachment = new FileAttachment();
      fAttachment.Load("D:\\Stream" + fAttachment.Name);
    }

怎么样ItemAttachment?我们如何将这样的内容保存ItemAttachment在指定的文件中?

4

1 回答 1

14

当然,这仍然不是一个紧迫的问题,但我想我会和我一样在未来偶然发现这个问题的人分享。

对于 ItemAttachments,您需要为该项目加载 MimeContent,然后您可以简单地写入文件/输出 [".eml", ".msg"]:

if (attachment is FileAttachment)
{
    FileAttachment fileAttachment = attachment as FileAttachment;

    // Load attachment contents into a file.
    fileAttachment.Load(<file path>);
}
else // Attachment is an ItemAttachment (Email)
{
    ItemAttachment itemAttachment = attachment as ItemAttachment;

    // Load Item with additionalProperties of MimeContent
    itemAttachment.Load(EmailMessageSchema.MimeContent);

    // MimeContent.Content will give you the byte[] for the ItemAttachment
    // Now all you have to do is write the byte[] to a file
    File.WriteAllBytes(<file path>, itemAttachment.Item.MimeContent.Content);
}
于 2014-01-21T17:06:31.317 回答