HOWTO: EWS: Use GetAttachment to download attachments off Mail/Appointment

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值