2011-07-29 33 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值。