摘要
获取的邮件内容为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;
}
{
<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();
{
<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;"> pos;
</span><span style="color: #0000ff;">break</span><span style="color: #000000;">;
}
}
</span><span style="color: #0000ff;">if</span> (posToRemove < <span style="color: #800080;">0</span><span style="color: #000000;">)
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">false</span><span style="color: #000000;">;
index.Remove(posToRemove);
AdjustIndex(index,posToRemove,path.Length </span>- (CidPattern.Length +<span style="color: #000000;"> cid.Length));
</span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">true</span><span style="color: #000000;">;
}
获取的item中获取附件
FileAttachment[] attachments = index = (index.Count > && item.Attachments.Count > basePath =
}
}
}
原文链接:https://www.f2er.com/csharp/191530.htmlattachments </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> FileAttachment[item.Attachments.Count];
</span><span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">var</span> i = <span style="color: #800080;">0</span>; i < item.Attachments.Count; ++<span style="color: #000000;">i)
{
</span><span style="color: #0000ff;">var</span> type =<span style="color: #000000;"> item.Attachments[i].ContentType.ToLower();
</span><span style="color: #0000ff;">if</span> (!type.StartsWith(<span style="color: #800000;">"</span><span style="color: #800000;">image/</span><span style="color: #800000;">"</span>)) <span style="color: #0000ff;">continue</span><span style="color: #000000;">;
type </span>= type.Replace(<span style="color: #800000;">"</span><span style="color: #800000;">image/</span><span style="color: #800000;">"</span>,<span style="color: #800000;">""</span><span style="color: #000000;">);
</span><span style="color: #0000ff;">var</span> attachment =<span style="color: #000000;"> (FileAttachment)item.Attachments[i];
</span><span style="color: #0000ff;">var</span> cid =<span style="color: #000000;"> attachment.ContentId;
</span><span style="color: #0000ff;">var</span> filename = cid + <span style="color: #800000;">"</span><span style="color: #800000;">.</span><span style="color: #800000;">"</span> +<span style="color: #000000;"> type;
</span><span style="color: #0000ff;">var</span> path =<span style="color: #000000;"> Path.Combine(basePath,filename);
</span><span style="color: #0000ff;">if</span>(ReplaceCid(index,<span style="color: #0000ff;">ref</span><span style="color: #000000;"> sHTMLCOntent,cid,path))
{
</span><span style="color: #008000;">//</span><span style="color: #008000;"> only load images when they have been found </span>
<span style="color: #000000;"> attachment.Load(path);
attachments[i]
}
}
}