摘要

在exchange 2007或者2010中获取的邮件内容为html标签格式,也就是一个页面。如果里面含有img标签,你会发现img标签的src属性为cid:xxxxxxxxxxxx的一串字符串,并不是url,这时候就造成页面上图片显示不出来。

解决办法

在网上找了一种解决办法。

原文地址:http://stackoverflow.com/questions/6650842/ews-exchange-2007-retrieve-inline-images

首先创建cid的索引

private const string CidPattern = "cid:";

private static HashSet<int> BuildCidIndex(string html)
{
var index = new HashSet<int>();
var pos = html.IndexOf(CidPattern, );
while (pos > )
{
var start = pos + CidPattern.Length;
index.Add(start);
pos = html.IndexOf(CidPattern, start);
}
return index;
}

在索引的基础上封装一个替换的方法

private static void AdjustIndex(HashSet<int> index, int oldPos, int byHowMuch)
{
var oldIndex = new List<int>(index);
index.Clear();
foreach (var pos in oldIndex)
{
if (pos < oldPos)
index.Add(pos);
else
index.Add(pos + byHowMuch);
}
} private static bool ReplaceCid(HashSet<int> index, ref string html, string cid, string path)
{
var posToRemove = -;
foreach (var pos in index)
{
if (pos + cid.Length < html.Length && html.Substring(pos, cid.Length) == cid)
{
var sb = new StringBuilder();
sb.Append(html.Substring(, pos-CidPattern.Length));
sb.Append(path);
sb.Append(html.Substring(pos + cid.Length));
html = sb.ToString(); posToRemove = pos;
break;
}
} if (posToRemove < )
return false; index.Remove(posToRemove);
AdjustIndex(index, posToRemove, path.Length - (CidPattern.Length + cid.Length)); return true;
}

在获取的item中获取附件

FileAttachment[] attachments = null;
var index = BuildCidIndex(sHTMLCOntent);
if (index.Count > && item.Attachments.Count > )
{
var basePath = Directory.GetCurrentDirectory(); attachments = new FileAttachment[item.Attachments.Count];
for (var i = ; i < item.Attachments.Count; ++i)
{
var type = item.Attachments[i].ContentType.ToLower();
if (!type.StartsWith("image/")) continue;
type = type.Replace("image/", ""); var attachment = (FileAttachment)item.Attachments[i];
var cid = attachment.ContentId;
var filename = cid + "." + type;
var path = Path.Combine(basePath, filename);
if(ReplaceCid(index, ref sHTMLCOntent, cid, path))
{
// only load images when they have been found
attachment.Load(path);
attachments[i] = attachment;
}
}
}

[c#]获取exchange中的图片的更多相关文章

  1. c# 应用NPOI 获取Excel中的图片,保存至本地的算法

    要求:读取excel中的图片,保存到指定路径 思路:  利用NPOI中 GetAllPictures()方法获取图片信息 步骤: 1.新建一个Windows窗体应用程序 2.桌面新建一个excel,贴 ...

  2. C#获取Html中的图片元素路径

    使用Ueditor的时候把文章以HTML标签的方式存在数据库中,同时还要将文章的第一张图片的路径一并存入数据库,所以就需要在Html中获取第一个图片的路径,没有图片的话设置一个默认的图片.代码如下: ...

  3. 爬虫的原理获取html中的图片到本地

    如果你想获取哪个网页的图片,如果你想知道那个网址的美女,还等什么.代码走起:下载即可使用 完成这次瞎爬的原理如下: 第一步:获取html内容 * 第二步:然后在获取的html文本中寻找图片,根据htm ...

  4. Java 获取Word中指定图片的坐标位置

    本文介绍通过Java程序获取Word文档中指定图片的坐标位置. 程序运行环境: Word测试文档:.docx 2013 Free Spire.doc.jar 3.9.0 IntelliJ IDEA J ...

  5. Android Post方式发送信息和获取URL中的图片

    需要Internet权限,AndroidManifest.xml <uses-permission android:name="android.permission.INTERNET& ...

  6. Java jsoup获取网页中的图片

    获取图片 package com.vfsd.net; import java.io.File; import java.io.FileOutputStream; import java.io.IOEx ...

  7. 获取Excel中的图片

    如下图,上传要获取这里面的图片,而又不能直接选择,怎么办呢? 1.首先复制一份Excel文件命名copy.xlsx 2.修改copy.xlsx文件的后缀名变成copy.rar 3.解压copy.rar ...

  8. 如何获取xcassets中LaunchImage图片

    NSDictionary * dic = @{@"320x480" : @"LaunchImage-700", @"320x568" : @ ...

  9. iOS webview 获取html中的图片地址

    //js代码,声明1个数组,对img 进行遍历,采用,分割多个url NSString * getAllImages = @"var str = new Array();" &qu ...

随机推荐

  1. bzoj3743: [Coci2015]Kamp

    首先树dp求出一个点的答案 然后再一遍dfs换根(是叫做换根吗.. 详见代码 #include <iostream> #include <cstdio> #include &l ...

  2. ffmpeg 转成MP3采样率8000

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. [cf621E]Wet Shark and Blocks

    Description 给定$n$个数和$b$个盒子,放一些数到盒子中,使得盒子不为空.每个盒子中的数是一样的,一个数可以被放到多个盒子中. 从每个盒子中取一个数,组成一个$b$位数,如果这个数$mo ...

  4. NameNode & DataNode

    NameNode类位于org.apache.hadoop.hdfs.server.namenode包下. NameNode serves as both directory namespace man ...

  5. NuGet多个项目依赖的公共组件如何打包

    会有这样一种情况:在同一个解决方案下面,类库A是独立的,类库B是依赖于类库A的:类似这样: 所以在使用类库B时必须引入类库A的东西,这时如果作为nuget包打包发布,有如下的解决思路: 1.在整个解决 ...

  6. 主机宝(zhujibao) /a/apps/zhujibao/manager/apps/config/config.php no-password Login Vulnerabilities Based On Default cookie Verification From Default File

    catalog . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 主机宝管理程序使用了CodeIgniter框架,要想在CodeIgnit ...

  7. 数据结构算法C语言实现(一)---2.2线性表的顺序表示和实现

    注意: 虽然是用C语言实现,但是考虑到使用了一个C++的特性----引用以简化代码,所以所有的代码均以cpp作为后缀,用g++编译(以后不做说明). g++版本: 一.简述 本节主要讲述线性表的顺序实 ...

  8. 【Alpha版本】 第七天 11.15

    一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 昨天完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 完成我要招聘的招聘详情显示,完成简历填写及显示功能 完成我要应聘的 ...

  9. IOS - CORE DATA的目录(xcode6)

       当使用coredata作为app的后台数据存储介质后,我们很想知道数据是否成功插入.为此,我想找到coredata.sqlite的文件 代码中指定的存储目录为: - (NSURL *)appli ...

  10. hdu 2042 - 不容易系列之二

    题意:(略)太长了 解法:找规律,发现A[i]=A[i-1]*2-2; 1: #include<stdlib.h> 2: #include<string.h> 3: #incl ...