[c#]获取exchange中的图片

前端之家收集整理的这篇文章主要介绍了[c#]获取exchange中的图片前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

摘要

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

解决办法

解决办法。

CidPattern = <span style="color: #0000ff;">private <span style="color: #0000ff;">static HashSet<<span style="color: #0000ff;">int> BuildCidIndex(<span style="color: #0000ff;">string<span style="color: #000000;"> html)
{
<span style="color: #0000ff;">var
index = <span style="color: #0000ff;">new HashSet<<span style="color: #0000ff;">int><span style="color: #000000;">();
<span style="color: #0000ff;">var pos = html.IndexOf(CidPattern,<span style="color: #800080;">0<span style="color: #000000;">);
<span style="color: #0000ff;">while (pos > <span style="color: #800080;">0<span style="color: #000000;">)
{
<span style="color: #0000ff;">var start = pos +<span style="color: #000000;"> CidPattern.Length;
index.Add(start);
pos =<span style="color: #000000;"> html.IndexOf(CidPattern,start);
}
<span style="color: #0000ff;">return<span style="color: #000000;"> index;
}

方法

AdjustIndex(HashSet<> index, oldPos, oldIndex = List<> ( pos (pos <+<span style="color: #0000ff;">private <span style="color: #0000ff;">static <span style="color: #0000ff;">bool ReplaceCid(HashSet<<span style="color: #0000ff;">int> index,<span style="color: #0000ff;">ref <span style="color: #0000ff;">string html,<span style="color: #0000ff;">string cid,<span style="color: #0000ff;">string<span style="color: #000000;"> path)
{
<span style="color: #0000ff;">var
posToRemove = -<span style="color: #800080;">1
<span style="color: #000000;">;
<span style="color: #0000ff;">foreach
(<span style="color: #0000ff;">var
pos <span style="color: #0000ff;">in
<span style="color: #000000;"> index)
{
<span style="color: #0000ff;">if
(pos + cid.Length < html.Length && html.Substring(pos,cid.Length) ==<span style="color: #000000;"> cid)
{
<span style="color: #0000ff;">var
sb = <span style="color: #0000ff;">new<span style="color: #000000;"> StringBuilder();
sb.Append(html.Substring(<span style="color: #800080;">0,pos-<span style="color: #000000;">CidPattern.Length));
sb.Append(path);
sb.Append(html.Substring(pos +<span style="color: #000000;"> cid.Length));
html =<span style="color: #000000;"> sb.ToString();

        posToRemove </span>=<span style="color: #000000;"&gt; pos;
        </span><span style="color: #0000ff;"&gt;break</span><span style="color: #000000;"&gt;;
    }
}

</span><span style="color: #0000ff;"&gt;if</span> (posToRemove < <span style="color: #800080;"&gt;0</span><span style="color: #000000;"&gt;)
    </span><span style="color: #0000ff;"&gt;return</span> <span style="color: #0000ff;"&gt;false</span><span style="color: #000000;"&gt;;

index.Remove(posToRemove);
AdjustIndex(index,posToRemove,path.Length </span>- (CidPattern.Length +<span style="color: #000000;"&gt; cid.Length));

</span><span style="color: #0000ff;"&gt;return</span> <span style="color: #0000ff;"&gt;true</span><span style="color: #000000;"&gt;;

}

获取的item中获取附件

FileAttachment[] attachments = index = (index.Count > && item.Attachments.Count > basePath =attachments </span>= <span style="color: #0000ff;"&gt;new</span><span style="color: #000000;"&gt; FileAttachment[item.Attachments.Count]; </span><span style="color: #0000ff;"&gt;for</span> (<span style="color: #0000ff;"&gt;var</span> i = <span style="color: #800080;"&gt;0</span>; i < item.Attachments.Count; ++<span style="color: #000000;"&gt;i) { </span><span style="color: #0000ff;"&gt;var</span> type =<span style="color: #000000;"&gt; item.Attachments[i].ContentType.ToLower(); </span><span style="color: #0000ff;"&gt;if</span> (!type.StartsWith(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;image/</span><span style="color: #800000;"&gt;"</span>)) <span style="color: #0000ff;"&gt;continue</span><span style="color: #000000;"&gt;; type </span>= type.Replace(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;image/</span><span style="color: #800000;"&gt;"</span>,<span style="color: #800000;"&gt;""</span><span style="color: #000000;"&gt;); </span><span style="color: #0000ff;"&gt;var</span> attachment =<span style="color: #000000;"&gt; (FileAttachment)item.Attachments[i]; </span><span style="color: #0000ff;"&gt;var</span> cid =<span style="color: #000000;"&gt; attachment.ContentId; </span><span style="color: #0000ff;"&gt;var</span> filename = cid + <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;.</span><span style="color: #800000;"&gt;"</span> +<span style="color: #000000;"&gt; type; </span><span style="color: #0000ff;"&gt;var</span> path =<span style="color: #000000;"&gt; Path.Combine(basePath,filename); </span><span style="color: #0000ff;"&gt;if</span>(ReplaceCid(index,<span style="color: #0000ff;"&gt;ref</span><span style="color: #000000;"&gt; sHTMLCOntent,cid,path)) { </span><span style="color: #008000;"&gt;//</span><span style="color: #008000;"&gt; only load images when they have been found </span>

<span style="color: #000000;"> attachment.Load(path);
attachments[i] =<span style="color: #000000;"> attachment;
}
}
}

原文链接:https://www.f2er.com/csharp/191530.html

猜你在找的C#相关文章