簡體   English   中英

C#如何從URL中獲取CID(內容ID) <img src=“url.jpg” /> 標簽

[英]C# How to obtain cid (content id) from url in a <img src=“url.jpg” /> tag

我必須發送一個Multipart MailMessage,其正文是從html頁開始創建的。 問題是我必須“翻譯”以這種方式編寫的圖像標簽

<img src="url.jpg" />

到這種多部分標簽

<img src="cid:imageid" />

考慮到在替換此文本之前,我必須捕獲每個圖像URL並為每個圖像URL創建一個新的LinkedResource實例,您是否知道是否有任何工具可以為我完成此工作?

我正在使用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");

之后,您必須迭代cids集合並加載和附加每個文件作為鏈接資源,並將ContentID設置為剛剛設置的cid值。

我是這個論壇的新手,並且正在使用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,則在發送此電子郵件時會自動完成。

希望這可以解決您的問題,或者至少給您一個想法。

關於虛擬機

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM