需要用链接资源替换html中的嵌套图像,以便可以通过电子邮件发送

时间:2018-06-11 12:46:14

标签: c# html html-agility-pack

我使用WYSWYG编辑器制作html,然后发送电子邮件。但是当我在电子邮件正文中粘贴图像时,图像不会显示在电子邮件中。根据建议,我试图将所有img标签src转换为链接资源,如下所示:

var htmlString = "<ul><li class='ql - indent - 1'>Testing Body Text</li></ul><p>Helllloz</p><p><br></p><p><br></p><p><br></p><p><img src='data: image / png; base64,iVBORw0KGgoAAAANSUhEUgAAASUAAADWCAYAAACXFpR0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAO8SURBVHhe7dhLcpsAEEBBkfvfmUgVuRLHBWjhEs9M90ZsNTCPz7Le3QAifj1 / ARJECUgRJSBFlIAUUQJSRAlIESUgRZSAFFECUkQJSBElIEWUgBRRAlJECUgRJSBFlIAUUQJSRAlIESUgRZSAFFECUkQJSBElIEWUgBRRAlJECUgRJSBFlIAUUQJSRAlIESUgRZSAFFECUkQJSBElIEWUgBRRAlJECUgRJSBFlIAUUQJSRAlIESUgRZSAFFECUkQJSBElIEWUgBRRAlJECUgRJSBFlIAUUQJSRAlIESUgZVnvnsdELcvyPLoulyEfRClqQoi2uCRnE6WYyTH6n0tzJt + UQgTps8c8zGQeUYqwfNvMZhavbyd7deGufJrMgH + J0smOFnLa6TEPROlEews4 / bSYzVy + KZ3E0u3bm8HR0xQ / myidQJBeI0wziVKIIH1lJvOI0ptt3eEt37at2XhauiZRAlJEKcBT0jEzmkOU3sjrxvcz0 + sRpZN5AnidWc0gSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIoonWxZlucRR8xqBlF6o3Vdn0d8FzO9HlECUkQpwGvJMTOaQ5TebOt1w9Jt25qNV7drEqUQYfrKTOYRpRPs3eEt4V97s / CUdF2idBJh2idIc4lS1GMpJ8bp6H8L0vWJ0oleWbApcZryPzm23BfDrSfAQu5zmc7hSSnC0m0zm1lEKcTyffaYh5nMI0oxFvEPM5jLN6UfYML3JpchH0QJSPH6BqSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEpIgSkCJKQIooASmiBKSIEpAiSkCKKAEpogSkiBKQIkpAiigBKaIEhNxuvwEkqqSEml3B0AAAAABJRU5ErkJggg == '></p>";

        HtmlDocument document = new HtmlDocument();
        document.LoadHtml(htmlString);
        var nodes = document.DocumentNode.Descendants("img")
                         .Where(e =>
                         {
                             string src = e.GetAttributeValue("src", null) ?? "";
                             return !string.IsNullOrEmpty(src) && src.StartsWith("data: image");
                         })
                         .ToList();

        nodes.ForEach(x =>
                        {
                            string currentSrcValue = x.GetAttributeValue("src", null);
                            currentSrcValue = currentSrcValue.Split(',')[1];//Base64 part of string
                             byte[] imageData = Convert.FromBase64String(currentSrcValue);
                            string contentId = Guid.NewGuid().ToString();
                            LinkedResource inline = new LinkedResource(new MemoryStream(imageData), "image/png");
                            inline.ContentId = contentId;
                            inline.TransferEncoding = TransferEncoding.Base64;

                            x.SetAttributeValue("src", "cid:" + inline.ContentId);
                        });


        string result = document.DocumentNode.OuterHtml;

        System.IO.File.WriteAllText(@"F:\testimage.html", result);

当我看到html文件时,图像也没有显示。我正在使用htmlagilitypack包来替换图像。

有人可以sugegst吗?

0 个答案:

没有答案