当前位置: 首页 > news >正文

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

相关文章:

  • 使用 .NET 2.0 SecureString 类保护敏感数据
  • PowerShell的CmdLet开发的HelloWorld
  • Windows PowerShell学习笔记之一——命令帮助
  • C#的String.Format格式化举例(转)
  • .net(C#)中String.Format如何使用
  • ASP.net中名称以无效字符开头问题解决?
  • log4net简易配置--
  • c开发 一个关于访问性的问题
  • FCKedit(asp.net)详细配置以及下载
  • 如何生成一个新的GUID 全球统一唯一编号
  • readonly vs. const [C#] .net
  • VS2005 常用快捷键
  • ASP.NET中IsPostBack属性
  • 模仿Gmail右上角的Loading
  • 什么是数字信封?
  • JavaScript 如何正确处理 Unicode 编码问题!
  • 【css3】浏览器内核及其兼容性
  • 07.Android之多媒体问题
  • css属性的继承、初识值、计算值、当前值、应用值
  • ES学习笔记(10)--ES6中的函数和数组补漏
  • express + mock 让前后台并行开发
  • jdbc就是这么简单
  • Js基础知识(四) - js运行原理与机制
  • Markdown 语法简单说明
  • Terraform入门 - 3. 变更基础设施
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • unity如何实现一个固定宽度的orthagraphic相机
  • webgl (原生)基础入门指南【一】
  • 程序员最讨厌的9句话,你可有补充?
  • 对象管理器(defineProperty)学习笔记
  • 技术攻略】php设计模式(一):简介及创建型模式
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 三栏布局总结
  • 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!
  • 树莓派 - 使用须知
  • 微信小程序--------语音识别(前端自己也能玩)
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 用Canvas画一棵二叉树
  • #NOIP 2014# day.2 T2 寻找道路
  • #NOIP 2014#day.2 T1 无限网络发射器选址
  • #stm32整理(一)flash读写
  • $.extend({},旧的,新的);合并对象,后面的覆盖前面的
  • $forceUpdate()函数
  • (145)光线追踪距离场柔和阴影
  • (delphi11最新学习资料) Object Pascal 学习笔记---第8章第5节(封闭类和Final方法)
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (强烈推荐)移动端音视频从零到上手(上)
  • (算法)前K大的和
  • (原創) 是否该学PetShop将Model和BLL分开? (.NET) (N-Tier) (PetShop) (OO)
  • **PHP二维数组遍历时同时赋值
  • .NET 5.0正式发布,有什么功能特性(翻译)
  • .NET C#版本和.NET版本以及VS版本的对应关系
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .NET/C# 将一个命令行参数字符串转换为命令行参数数组 args
  • .netcore 如何获取系统中所有session_ASP.NET Core如何解决分布式Session一致性问题