Exchange Web Service 获取邮件的附件并保存到本地的示例代码
2024-10-11 14:47:45
private static void DownLoadMailAttachments(ExchangeService service, ItemId itemId)
{
EmailMessage message = EmailMessage.Bind(service, itemId, new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.Attachments)); ; if (message.HasAttachments)
{
foreach (MailAttachment attachment in message.Attachments)
{
FileAttachment fileAttachment = attachment as FileAttachment; ItemAttachment itemAttachment = attachment as ItemAttachment; if (itemAttachment != null)
{
itemAttachment.Load(EmailMessageSchema.MimeContent); char[] invalidChars = Path.GetInvalidPathChars();
string name = itemAttachment.Name; foreach (char invalidChar in invalidChars)
{
name = name.Replace(invalidChar, ' ');
} name = name.Replace(":", " "); string emailPath = Path.Combine(Path.GetTempPath(), name + ".eml"); using (FileStream stream = File.Open(emailPath, FileMode.Create, FileAccess.ReadWrite))
{
foreach (byte content in itemAttachment.Item.MimeContent.Content)
{
stream.WriteByte(content);
}
} } if (fileAttachment != null)
{
string filePath = Path.Combine(Path.GetTempPath(), fileAttachment.Name); fileAttachment.Load(); using (FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.ReadWrite))
{
foreach (byte content in fileAttachment.Content)
{
stream.WriteByte(content);
}
}
}
}
} }
最新文章
- spring静态代理
- [nRF51822] 7、基础实验代码解析大全(前十)
- android NDK debug 遇到的问题与解决方法
- Ueditor 编译发布后无法使用上传图片、附件等功能
- 关于OpenVPN的入门使用
- js/jquery 实时监听输入框值变化的完美方案:oninput &; onpropertychange
- 【python】list。列表
- Scala第四章学习笔记(面向对象编程)
- java socket 单服务器多客户端实时通信
- 给产品经理讲技术,不得不懂的TCP和UDP
- HDU_1071——积分求面积,抛物线顶点公式
- (转)[老老实实学WCF] 第四篇 初探通信--ChannelFactory
- 存储、读取——Android应用程序内置的文件夹
- MySql单表最大8000W+ 之数据库遇瓶颈记
- Ubuntu16.04+CUDA8.0+CUNN5.1+caffe+tensorflow+Theano
- ThinkPHP的Rbac权限控制
- idea为tomcat设置虚拟地址
- 手动(原生ajax)和自动发送ajax请求 伪ajax(Ifrname)
- CF912E Prime Gift
- C语言基础:分支语句和常见运算符 分类: iOS学习 c语言基础 2015-06-10 21:44 13人阅读 评论(0) 收藏