Get Attachments from a Message

Standard

In order to use this code you will need to get the GetBinding() method and understand how to get a message item UniqueId

GET ATTACHMENT’S COLLECTION

// Bind to an existing MyMessage item, requesting its 
//Id property plus its attachments collection.
EmailMessage myMessage = EmailMessage.Bind(GetBinding(),
    new ItemId(“Your message unique id”),
        new PropertySet(BasePropertySet.IdOnly,
            ItemSchema.Attachments));

ITERATE THROUGH ATTACHMENT’S COLLECTION

// Iterate through the attachments collection and load each attachment.
foreach (Attachment attachment in myMessage.Attachments)
{
}

GET THE FILE OR ITEM ATTACHMENT INTO MEMORY

if (attachment is FileAttachment)
{
    //If it's file attachment
    FileAttachment fileAttachment = attachment as FileAttachment;
    //Load the file attachment into memory
    fileAttachment.Load();
}
else // Attachment is an item attachment.
{
    //If it's item attachment 
    ItemAttachment itemAttachment = attachment as ItemAttachment;
    //Load the item attachment into memory
    itemAttachment.Load();
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s