I have fallen for exchange web services. There are endless possibilities with exchange web services, and product group is still working to make it even better.
Today I have created a neat sample to download attachments off Exchange Server
Input Params: itemID , folder
public void DownloadAttachments(string itemID,string folder)
ExchangeServiceBinding esb = new ExchangeServiceBinding();
esb.AllowAutoRedirect = true;
esb.Credentials = new System.Net.NetworkCredential("USERNAME", "PASSWORD", "DOMAIN");
esb.Url = "http://your-cas-server/ews/exchange.asmx";
//first we need to get the attachment IDs for the item so we will need to make a GetItem call first
//specify the conetent that we want to retrieve
PathToUnindexedFieldType ptufta = new PathToUnindexedFieldType;
ptufta = new PathToUnindexedFieldType();
ptufta.FieldURI = UnindexedFieldURIType.itemAttachments;
ptufta = new PathToUnindexedFieldType();
ptufta.FieldURI = UnindexedFieldURIType.itemHasAttachments;
ItemResponseShapeType irst = new ItemResponseShapeType();
irst.BaseShape = DefaultShapeNamesType.IdOnly;
irst.AdditionalProperties = ptufta;
ItemIdType biita = new ItemIdType;
biita = new ItemIdType();
biita.Id = itemID;
//get the items
GetItemType git = new GetItemType();
git.ItemShape = irst;
git.ItemIds = biita;
GetItemResponseType girt = esb.GetItem(git);
if (girt.ResponseMessages.Items.ResponseClass != ResponseClassType.Success)
//now that we have the attachment IDs let's request the atthacments and save them to disk
ItemType MsgItem = ((ItemInfoResponseMessageType)girt.ResponseMessages.Items).Items.Items;
AttachmentResponseShapeType arst = null;
AttachmentIdType aita = null;
if (true == MsgItem.HasAttachments)
//create the attachment shape; we want the mime contnet just in case this is an message item so that we can save to disk
arst = new AttachmentResponseShapeType();
arst.IncludeMimeContent = true;
arst.IncludeMimeContentSpecified = true;
//create an array of attachment ids that we want to request
aita = new AttachmentIdType[MsgItem.Attachments.Length];
for (int i = 0; i < MsgItem.Attachments.Length; i++)
aita[i] = new AttachmentIdType();
aita[i].Id = MsgItem.Attachments[i].AttachmentId.Id;
//create a GetAttachment object for the GetAttachment operation
GetAttachmentType gat = new GetAttachmentType();
gat.AttachmentIds = aita;
gat.AttachmentShape = arst;
GetAttachmentResponseType gart = esb.GetAttachment(gat);
//save each attachment to disk
foreach (AttachmentInfoResponseMessageType Attachment in gart.ResponseMessages.Items)
//attachments can be of type FileAttachmentType or ItemAttachmentType
//so we need to figure out which type we have before we manipulate it
//save to disk
FileAttachmentType TheFileAttachment = (FileAttachmentType)Attachment.Attachments;
using (Stream FileToDisk = new FileStream(folder + @"\" + Attachment.Attachments.Name, FileMode.Create))
ItemType TheItemAttachment = ((ItemAttachmentType)Attachment.Attachments).Item;
using (Stream FileToDisk = new FileStream(folder + @".\" + Attachment.Attachments.Name + ".eml", FileMode.Create))
byte ContentBytes = System.Convert.FromBase64String(TheItemAttachment.MimeContent.Value);
Keywords: GetAttachment, download attachments, exchange web services, exchange 2007
thanks for your article.....
That's what i want
How to get in-line Image in message body?
this is in-line attachment (below attid0):
this is attachmentID
please teach me how get in-line image in messagebody using Exchange Web Service
How can I get the from-email-address?
The GetItemResponse include following tags,
but I can't find getter methods for that:
Great article... How are you obtaining the itemID that you are passing in. I've tried numerous ID's that I'm getting from the message which all return a malformedID response code.
The itemId is unique Base64 encoded string value, which represents a unique item in store. You can use FindItem to find a particular item in store and then call GetItem on the ItemID returned by that FindItem call.
Refer to the following post which has sample code for GetFolder, FindItem, GetItem and how to use restrictions with FindItem
Friends, I am new to the world of exchange server 2007, can anyone suggest me that how can I proceede to fetch mailbox details (Subject / has attachments, if yes attachment details..) Thanks.
This was EXACTLY what I needed!
Thank You A Million!!
You are a genius!
This code works great unless there's a inline image (i.e. signature logo). But how can I detect if it's an inline image or not. The book inside Microsoft Exchange Server 2007 Web Services says that Exchange Web Services doesn't provide a way to determine the visibility of an attachment. What's the right way to handle this so we can get all attachments?
Just had to say thank you. Wish I found this page 4 hours ago!!
I always get following error when i try to use this method: "The request failed with HTTP status 403: Forbidden"
Do you know if I'm doing something wrong or passing through the wrong ID? I tried with
But always the same error
@Kobe: Did you use https://... to connect to your Exchange server ?
@Vikas: Nice article, works great! But the code indeed fails if inline attachments are detected, because the MsgItem.HasAttachments is FALSE. So the attachment id is not defined and esb.GetAttachment(gat) fails.
I'll try to fix this but I am not sure wether I understand EWS good enough to achieve this.
Great! Had the information I was after. Thanks!
Download Attachment link is not working..
I am using EWS with SearchFilterCollection, and the Inbox I am checking should have 4 emails that are in scope, 3 with 0, 1, or multiple file attachments, and 1 with an item attachment.
My trouble is that when I apply my searchfilter (> searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.HasAttachments, true)); <) I don't get the one with the email <Item> attachment
Is there a special searchFilter for this?
I am trying to use your code with Exchange 2010/EWS, on c#, and the ItemType is not recognized:
This line won't compile:
what could I do to fix this?