如何使用EWS托管API保存ItemAttachments

use*_*567 6 c# exchangewebservices ews-managed-api

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

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

为了ItemAttachment什么?我们如何ItemAttachment在指定的文件中保存这样的?

小智 10

当然这不是一个紧迫的问题,但我想我会分享给未来的人,就像我一样.

对于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);
}
Run Code Online (Sandbox Code Playgroud)