2011-07-29 29 views
0

获取cid(content id)我必须发送一个Multipart MailMessage,它的主体是从html页面开始创建的。 的问题是,我必须这样写的图像标签C#如何从URL <img src="url.jpg" />标记

<img src="url.jpg" /> 

“翻译”,以这种

<img src="cid:imageid" /> 

的多标签考虑到我要抓住每一个图像URL和创建新的LinkedResource实例为每个人做这个文本替换之前,你知道是否有任何工具可以帮我做这个工作吗?

回答

0

我是新来这个论坛和使用MS Outlook对象库。那么,我也一直坚持这个愚蠢的问题。我记得很久以前就读过它,但没能再找到那篇文章。

反正这里是你可以在这个时刻做什么,这是我在做什么也只要你保存它(请接受VB而不是C#的答案)

<Code> 
EmailItem = Mailitem 
EmailItem.HTMLBODY = </img src = "SOMETHING.jpg" ...../> 
EmailItem.Save 
EmailItem.HTMLBODY >>> Read this text and parse it for CID of the image tag. 
</Code> 

,它被转换到CID(给它唯一的内容ID)。解析并重建您的HTML。 这是一个很长的路,但现在解决这个问题的一个可行的方法。

此外,如果图片托管在公共网络上,则无需将其更改为CID,它将在您发送此电子邮件时自动完成。

希望这可以解决您的问题,或者可能会给你一个想法。

问候 Virender

1

我使用HtmlAgilityPack这使得更换IMG SRC值很简单:

Dictionary<string, string> cids = new Dictionary<string, string>(); 
int imgCount = 0; 
HtmlDocument doc = new HtmlDocument(); 
doc.Load(htmlFilename, System.Text.Encoding.UTF8); 
foreach(HtmlNode link in doc.DocumentNode.SelectNodes("//img")) 
{ 
    HtmlAttribute att = link.Attributes["src"]; 
    Console.Write("img src = " + att.Value); 
    if (!cids.ContainsKey(att.Value)) 
    { 
     cids[att.Value] = imgCount.ToString() + "." + GetRandomString(10) + "@domain.com"; 
     imgCount++; 
    } 
    att.Value = "cid:" + cids[att.Value]; 
    Console.WriteLine(" became " + att.Value); 
} 
StringWriter sw = new StringWriter(); 
doc.Save(sw); 
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(sw.ToString(), System.Text.Encoding.GetEncoding("utf-8"), "text/html"); 

在这之后,你必须遍历的CID收集和装载每个文件附加为链接的资源,将ContentID设置为刚刚设置的cid值。