I look it very usefull, so save it....
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
Sample: DownloadAttachments
Input Params: itemID, folder
publicvoidDownloadAttachments(stringitemID,stringfolder)
{
ExchangeServiceBindingesb = newExchangeServiceBinding();
esb.AllowAutoRedirect = true
esb.Credentials = newSystem.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 = newPathToUnindexedFieldType[2];
ptufta[0] = newPathToUnindexedFieldType();
ptufta[0].FieldURI = UnindexedFieldURIType.itemAttachments;
ptufta[1] = newPathToUnindexedFieldType();
ptufta[1].FieldURI = UnindexedFieldURIType.itemHasAttachments;
ItemResponseShapeTypeirst = newItemResponseShapeType();
irst.BaseShape = DefaultShapeNamesType.IdOnly;
irst.AdditionalProperties = ptufta;
ItemIdType[] biita = newItemIdType[1];
biita[0] = newItemIdType();
biita[0].Id = itemID;
//get the items
GetItemTypegit = newGetItemType();
git.ItemShape = irst;
git.ItemIds = biita;
GetItemResponseTypegirt = esb.GetItem(git);
if(girt.ResponseMessages.Items[0].ResponseClass != ResponseClassType.Success)
return
//now that we have the attachment IDs let's request the atthacments and save them to disk
ItemTypeMsgItem = ((ItemInfoResponseMessageType)girt.ResponseMessages.Items[0]).Items.Items[0];
AttachmentResponseShapeTypearst = 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 = newAttachmentResponseShapeType();
arst.IncludeMimeContent = true
arst.IncludeMimeContentSpecified = true
//create an array of attachment ids that we want to request
aita = newAttachmentIdType[MsgItem.Attachments.Length];
for(inti = 0; i
{
aita[i] = newAttachmentIdType();
aita[i].Id = MsgItem.Attachments[i].AttachmentId.Id;
}
}
//create a GetAttachment object for the GetAttachment operation
GetAttachmentTypegat = newGetAttachmentType();
gat.AttachmentIds = aita;
gat.AttachmentShape = arst;
GetAttachmentResponseTypegart = esb.GetAttachment(gat);
//save each attachment to disk
foreach(AttachmentInfoResponseMessageTypeAttachment ingart.ResponseMessages.Items)
{
switch(Attachment.Attachments[0].GetType().Name)
{
//attachments can be of type FileAttachmentType or ItemAttachmentType
//so we need to figure out which type we have before we manipulate it
case"FileAttachmentType":
//save to disk
FileAttachmentTypeTheFileAttachment = (FileAttachmentType)Attachment.Attachments[0];
using(StreamFileToDisk = newFileStream(folder + @"/"+ Attachment.Attachments[0].Name, FileMode.Create))
{
FileToDisk.Write(TheFileAttachment.Content, 0,
TheFileAttachment.Content.Length);
FileToDisk.Flush();
FileToDisk.Close();
}
break
case"ItemAttachmentType":
//save to disk
ItemTypeTheItemAttachment = ((ItemAttachmentType)Attachment.Attachments[0]).Item;
using(StreamFileToDisk = newFileStream(folder + @"./"+ Attachment.Attachments[0].Name + ".eml", FileMode.Create))
{
byte[] ContentBytes = System.Convert.FromBase64String(TheItemAttachment.MimeContent.Value);
FileToDisk.Write(ContentBytes, 0,
ContentBytes.Length);
FileToDisk.Flush();
FileToDisk.Close();
}
break
default:
break
}
}
}
本文转自
http://blogs.msdn.com/vikas/archive/2007/10/16/howto-ews-use-getattachment-to-download-attachments-off-mail-appointment.aspx?CommentPosted=true#commentmessage