How to save ItemAttachments using the EWS Managed API

Can I save an ItemAttachment ? For FileAttachment we use the following EWS Managed API Code to save,

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

What about ItemAttachment ? How to save ItemAttachment like this in the specified file?

+4
source share
1 answer

Of course, this is not yet relevant, but I believe that I will share with those who stumble about it in the future, as I do.

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); } 
+7
source

All Articles