1. private static void DownLoadMailAttachments(ExchangeService service, ItemId itemId)
  2. {
  3. EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments)); ;
  4.  
  5. if (message.HasAttachments)
  6. {
  7. foreach (MailAttachment attachment in message.Attachments)
  8. {
  9. FileAttachment fileAttachment = attachment as FileAttachment;
  10.  
  11. ItemAttachment itemAttachment = attachment as ItemAttachment;
  12.  
  13. if (itemAttachment != null)
  14. {
  15. itemAttachment.Load(EmailMessageSchema.MimeContent);
  16.  
  17. char[] invalidChars = Path.GetInvalidPathChars();
  18. string name = itemAttachment.Name;
  19.  
  20. foreach (char invalidChar in invalidChars)
  21. {
  22. name = name.Replace(invalidChar, ' ');
  23. }
  24.  
  25. name = name.Replace(":", " ");
  26.  
  27. string emailPath = Path.Combine(Path.GetTempPath(), name + ".eml");
  28.  
  29. using (FileStream stream = File.Open(emailPath, FileMode.Create, FileAccess.ReadWrite))
  30. {
  31. foreach (byte content in itemAttachment.Item.MimeContent.Content)
  32. {
  33. stream.WriteByte(content);
  34. }
  35. }
  36.  
  37. }
  38.  
  39. if (fileAttachment != null)
  40. {
  41. string filePath = Path.Combine(Path.GetTempPath(), fileAttachment.Name);
  42.  
  43. fileAttachment.Load();
  44.  
  45. using (FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite))
  46. {
  47. foreach (byte content in fileAttachment.Content)
  48. {
  49. stream.WriteByte(content);
  50. }
  51. }
  52. }
  53. }
  54. }
  55.  
  56. }

Exchange Web Service 获取邮件的附件并保存到本地的示例代码的更多相关文章

  1. 使用 EWS(Exchange Web Service)协议读取邮件、发送邮件

    问题: 公司之前可以通过POP3协议收发邮件,因而在SoapUI中用JavaMail可以读取邮件,后来配置了Office 365,POP3协议端口不再开放,邮件全部读取失败,报login timeou ...

  2. php获取网页中图片并保存到本地

    php获取网页中图片并保存到本地的代码,将网页中图片保存本地文件夹: save_img("http://www.jbxue.com" ?>

  3. php获取网页中图片并保存到本地的代码

    php获取网页中图片并保存到本地的代码,将网页中图片保存本地文件夹: <?php /** * 获取网页中图片,并保存至本地 * by www.jbxue.com */ header(" ...

  4. Fork 多进程 模拟并行访问web service获取响应时间差

    #include <ros/ros.h> #include <iostream> #include <string> #include <cstring> ...

  5. 通过Places API Web Service获取兴趣点数据

    实验将爬取新加坡地区的银行POI数据 数据库采用mongodb,请自行安装,同时申请google的key 直接上代码 #coding=utf-8 import urllib import json i ...

  6. Android 获取截图 并将其保存到本地sd在卡路径

    /** * 获取当前屏幕和保存截图 */ private void GetandSaveCurrentImage() { //1.构建Bitmap WindowManager windowManage ...

  7. 如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]

    http://bbs.csdn.net/topics/390426978 public static String readCheckImage(HashMap<String, String&g ...

  8. wpf 获取Image的图片并保存到本地

    XMAL代码如下: <Image Name="ImageToSave" Source="Images/pic_bg.png" Grid.RowSpan=& ...

  9. 浅谈Exchange 2013开发-如何操作邮件的附件

    因为项目中客户有一个的要求,所以这个Exchange前段时间搞的我很是头疼,没接触过这个东西,但是现在看来,纸老虎一个.希望我的经验可以帮助初次接触它的人少走一些弯路! 简单介绍一下:客户要求在自己的 ...

随机推荐

  1. C# C++ Java接口类型转换

    最近这几天做了一个兼职 ,主要是把C语言以及C#语言封装的dll,经过C++中转为Java语言支持的,主要其中的问题就是类型转换,在此列出常用类型的转换过程. #include "stdaf ...

  2. distance.c

    #include "stdio.h" #include "string.h" #include "math.h" #include &quo ...

  3. 客户端数据持久化解决方案: localStorage

    客户端数据持久化解决方案: localStorage localStorage主要用来替代cookie,解决cookie读写困难.容量有限的问题. localStorage有以下几个特点 localS ...

  4. mongodb与mysql相比的优缺点

    与关系型数据库相比,MongoDB的优点:①弱一致性(最终一致),更能保证用户的访问速度:举例来说,在传统的关系型数据库中,一个COUNT类型的操作会锁定数据集,这样可以保证得到“当前”情况下的精确值 ...

  5. RHEL Channel Bonding

    1. 添加 kernel 模块 RHEL5上编辑 /etc/modprobe.conf 加入 alias bond0 bonding options bond0 miimon=100 mode=1   ...

  6. hdu 5583 Kingdom of Black and White(模拟,技巧)

    Problem Description In the Kingdom of Black and White (KBW), there are two kinds of frogs: black fro ...

  7. Colorbox cannot load the image added by js

    As we know, Colorbox is a wonderful js plugin. I came up against a head-banged problem in v1.5.6. Wh ...

  8. php获取服务器地址

    if ( isset( $_SERVER['HTTP_X_FORWARDED_HOST'] ) ) { // Support ProxyPass        $t_hosts = explode( ...

  9. 矩形嵌套(LIS)

    矩形嵌套 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a& ...

  10. 【Stackoverflow好问题】Java += 操作符实质

    问题 直到今天,我都一直以为: i += j 等同于 i = i + j; 但如果有: int i = 5; long j = 8; 这时 i = i + j不能编译.但i += j却能够编译.这说明 ...