user1891567
user1891567

Reputation: 701

How to save ItemAttachments using EWS Managed API

Is it possible to save an ItemAttachment? For FileAttachment we using the following EWS Managed API Code to save,

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

What about For ItemAttachment? How can we save the ItemAttachment like this in a specified file?

Upvotes: 8

Views: 7832

Answers (1)

Eric D
Eric D

Reputation: 506

Sure this is not still a pressing matter, but I figure I will share for anyone who stumbles across this in the future as I did.

For ItemAttachments you need to load the MimeContent for the item, then you can simply write to the file/output [".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);
}

Upvotes: 16

Related Questions