Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 680
user1891567 Avatar asked Feb 01 '13 11:02

user1891567


People also ask

Is the EWS API deprecated?

On October 5, 2021, the Microsoft Exchange Team BLOG posted a notice about Upcoming API Deprecations for EWS in Exchange Online. The article included some API commands becoming end of life. However, the true shock was a statement that on September 30, 2022, no new Application Registrations would be allowed.

What is EWS Managed API?

Microsoft Exchange Web Services (EWS) is a native API built by Microsoft that allows server/client applications to integrate with Exchange Servers and Office 365. Connecting Salesloft to Office 365 or Exchange Server through Microsoft's EWS API allows access to calendars and emails.

What is EWS in email?

Exchange Web Services (EWS) is a cross-platform API that enables applications to access mailbox items such as email messages, meetings, and contacts from Exchange Online, Exchange Online as part of Office 365, or on-premises versions of Exchange starting with Exchange Server 2007.


1 Answers

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);
}
like image 194
Eric D Avatar answered Oct 12 '22 22:10

Eric D