8

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?

user1891567
  • 681
  • 2
  • 10
  • 15

1 Answers1

16

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);
}
Eric D
  • 506
  • 3
  • 10
  • What version of Microsoft.Exchange.WebServices.dll do you use? – T.S. Apr 08 '14 at 16:42
  • Sorry for the delay. I am using Microsoft EWS Managed API 2.0: Information: Direct Link for Library: – Eric D Apr 16 '14 at 19:55
  • No problem. This `itemAttachment.Load(EmailMessageSchema.MimeContent);` what I was missing all along. – T.S. Apr 16 '14 at 20:53
  • The reason is: when you originally pull the item, it only grabs the base properties (or custom property set that you establish). Any other properties you want have to be loaded on-demand. – Eric D Apr 17 '14 at 13:38