Short solution for short problems

How to read an email adress (mail box) from an Exchange 2007 or 2010 server and download email attachments?

For this I created a very simple WPF program (Source code attached at the bottom of this post) that will read a certain email address and process some items that have a certain subject. It will then download all the attachments of the items to the local disk. In the beginning I had some trouble finding the attachments of the items, but this was because I didn’t force a Load on the items. This will load all attachment information. (Lazy Loading I guess)

ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1);


    //You can pass credentials of the creadetials running the program have no access to the mail box

    //service.Credentials = new NetworkCredential( "{Active Directory ID}", "{Password}", "{Domain Name}" );


    //Provide the mail box to read



    //Create a filter for the mails returned, in this case only the mails with SendFilesID= in the subject will be returned

    SearchFilter searchFilter = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "SendFilesID=");


    //Return the first 10 items

    FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox,searchFilter,new ItemView(10));




        //Loop over the items

        foreach (Item item in findResults.Items)


            //Show result

            txtResult.Text += item.Subject +Environment.NewLine;

            //This load is needed to fill all the properties of the item, if you do not use this, attachment information will be empty


            //Check if the item has attachments

            if (item.HasAttachments)


                //Loop over the attachments

                foreach (Attachment attachment in item.Attachments)


                    //Check if the attachemnts is a file

                    if (attachment is FileAttachment)


                        if (!Directory.Exists(("C:\\TEMP\\Attachments")))




                        FileAttachment fileAttachment = attachment as FileAttachment;

                        // Load the file attachment into memory


                        Console.WriteLine("Attachment name: " + fileAttachment.Name);

                        // Stream attachment contents into a file.

                        Stream theStream = File.Create("C:\\TEMP\\Attachments\\" + fileAttachment.Name); //new FileStream("D:\\Downloads\\Attachments\\" + fileAttachment.Name, FileMode.OpenOrCreate, FileAccess.ReadWrite);





                    else // Attachment is an item attachment.


                        // Load attachment into memory and write out the subject.

                        ItemAttachment itemAttachment = attachment as ItemAttachment;


                        Console.WriteLine("Subject: " + itemAttachment.Item.Subject);






    catch (Exception er)







TestReadEmail.zip (17.67 kb)