How to save ItemAttachments using EWS Managed API
Asked Answered
T

1

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?

Tetryl answered 1/2, 2013 at 11:48 Comment(1)
What version of Microsoft.Exchange.WebServices.dll do you use?Balliol
D
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);
}
Daviddavida answered 21/1, 2014 at 17:6 Comment(4)
What version of Microsoft.Exchange.WebServices.dll do you use?Balliol
Sorry for the delay. I am using Microsoft EWS Managed API 2.0: Information: <msdn.microsoft.com/en-us/library/office/…> Direct Link for Library: <microsoft.com/en-us/download/details.aspx?id=42022>Daviddavida
No problem. This itemAttachment.Load(EmailMessageSchema.MimeContent); what I was missing all along.Balliol
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.Daviddavida

© 2022 - 2024 — McMap. All rights reserved.