简体   繁体   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

I have to send a Multipart MailMessage, with its body being created starting from an html page. 我必须发送一个Multipart MailMessage,其正文是从html页开始创建的。 The problem is that i have to "translate" the image tag written this way 问题是我必须“翻译”以这种方式编写的图像标签

<img src="url.jpg" />

to a multipart tag of this kind 到这种多部分标签

<img src="cid:imageid" />

Considering that i have to catch every image url and create a new LinkedResource instance for everyone of it before doing this text replacing, do you know if there is any instrument that do this work for me? 考虑到在替换此文本之前,我必须捕获每个图像URL并为每个图像URL创建一个新的LinkedResource实例,您是否知道是否有任何工具可以为我完成此工作?

I am using HtmlAgilityPack which makes replacing img src values quite easy: 我正在使用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");

After this you have to iterate the cids collection and load and attach each file as a linked resource, setting ContentID to the cid value just set. 之后,您必须迭代cids集合并加载和附加每个文件作为链接资源,并将ContentID设置为刚刚设置的cid值。

I am new to this forum and using the MS Outlook object library. 我是这个论坛的新手,并且正在使用MS Outlook对象库。 Well, I am also stuck with this stupid problem since long. 好吧,长期以来,我也一直陷于这个愚蠢的问题。 I remember reading about it a long time ago but couldnt find that post again. 我记得很久以前读过有关它的内容,但无法再次找到该帖子。

Anyway here is what you can do at this moment, this is what I am doing too (Please accept the answer in VB instead of C#) 无论如何,这是您目前可以执行的操作,这也是我正在执行的操作(请接受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>

As soon as you save it, it is converted to CID (A unique content ID is given to it). 保存后,它将立即转换为CID(为其分配了唯一的内容ID)。 Parse it through and reconstruct your HTML. 通过解析并重建您的HTML。 This is a long way round, but a poosible way out of this problem now. 这是一个很长的路要走,但是现在可以解决这个问题。

Also, if the image is hosted on a public network no need to change it to CID it will be automatically done when you send this email. 另外,如果图像托管在公共网络上,则无需将其更改为CID,则在发送此电子邮件时会自动完成。

Hope this solves your problem or may be give you an idea atleast. 希望这可以解决您的问题,或者至少给您一个想法。

Regards Virender 关于虚拟机

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM