Hunting those elusive Public Folders using Exchange Web Services (Part 3)

In the last two posts (Part 1, Part 2) I've been going over some of the API calls to the Exchange Web Services, and so far we have retrieved an Item, but not any of the attachments held therein... So, lets go get that attachment!

We have an ItemType, which lets us know we have attachments, and now will even let us pull some basic properties but not to save... so, how do we save?

What we're (well, *I'm*) going to do is get the Item, then if it has attachments, save the attachments to my temp directory, so first lets get the Items:

static void GetItem( ExchangeServiceBinding esb, BaseItemIdType itemId )
{
    // Form the FindItem request
    GetItemType request = new GetItemType();
    request.ItemIds = new BaseItemIdType[] {itemId};
    request.ItemShape = new ItemResponseShapeType();
    request.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties;
 
    GetItemResponseType response = esb.GetItem( request );
    foreach ( ItemInfoResponseMessageType responseMessage in response.ResponseMessages.Items )
    {
        if( responseMessage.ResponseClass != ResponseClassType.Success )
            continue; 
 
        foreach ( ItemType item in responseMessage.Items.Items )
        {
            if(item.HasAttachments && item.Attachments != null)
                foreach( AttachmentType attachment in item.Attachments )
                {
                    Console.WriteLine( "\t" + attachment.Name );
                }
        }
    }
}

So, given the ItemId we create a GetItemRequest and then call the 'GetItem' method, this gives us a list of Items with that ID (though in our case - we hope there is only 1)... In this code, I then just go through the attachments in the Item (if there are any) and write out the name...

The next step is to actually *Get* the attachment (as at the moment, we only have the name, id etc)... sooo....

static void GetAttachment( ExchangeServiceBinding esb, RequestAttachmentIdType attachmentId )
{
    GetAttachmentType request = new GetAttachmentType();
    request.AttachmentIds = new RequestAttachmentIdType[] { attachmentId };
 
    GetAttachmentResponseType response = esb.GetAttachment( request );
    foreach( AttachmentInfoResponseMessageType responseMessage in response.ResponseMessages.Items)
    {
        if( responseMessage.ResponseClass != ResponseClassType.Success )
            return;
 
        foreach ( FileAttachmentType attachment in responseMessage.Attachments )
            using( BinaryWriter binWriter = new BinaryWriter( File.Open( @"C:\Temp\" + attachment.Name, FileMode.Create ) ) )
                binWriter.Write( attachment.Content );
    }
}

and with a quick modification of the 'foreach( AttachmentType attachment in item.Attachments) GetItem method to read:

foreach( AttachmentType attachment in item.Attachments )
{
    Console.WriteLine( "\t" + attachment.Name );
    GetAttachment( esb, attachment.AttachmentId );
}

We're saving all our attachments to the hard drive! Huzzah!

What's important to note in all these posts... The code isn't finalised (i.e. I haven't really added error checking etc) this is more my 'test' code for connecting, and whilst it works in my case, it may not in yours!

Print | posted @ Tuesday, December 16, 2008 10:47 AM

Comments on this entry:

Gravatar # re: Hunting those elusive Public Folders using Exchange Web Services (Part 3)
by jeremy at 8/6/2009 12:21 PM

Hi I have been searching high and low trying to figure out how to get the senders email address.
Something like the following, does not return a 'Name' or 'From' property that every example says it should have. Could you pleasepost a working example?
getItemResponseMessage.Items.Items(0).Subject
Gravatar # re: Hunting those elusive Public Folders using Exchange Web Services (Part 3)
by Umar Saleem at 8/24/2009 7:01 AM

Really nice & helpful article, thanks for posting such a good resource about accessing Exchange Data through Exchange Web Services...
Gravatar # re: Hunting those elusive Public Folders using Exchange Web Services (Part 3)
by Brian at 7/30/2010 6:36 AM

How do you process multiple attachments? attachment returns All the attachment names and ALL the content. If I have 4 attachments it will loop 4 times but will always process the last attachment in the group and write the last file. I can't seem to find a working example anywhere that correctly processes multiple attachments.
Gravatar # re: Hunting those elusive Public Folders using Exchange Web Services (Part 3)
by Brian at 7/30/2010 12:55 PM

Ok. I figured it out. For multiple attachments, you must maintain your own counter, and put the attachment.name results into a splitter array and then access the file name from the array based on your counter. Otherwise everything that is returned pertains to THAT attachment. Why Microsoft doesn't follow suit for the name is beyond me. Anyway it works now so thanks for your example! :)
Post A Comment
Title:
Name:
Email:
Comment:
Verification: