0

I am making html using a WYSWYG editor and then send in email. But when i paste images in email body then images doesn't get displayed in email. SO based on suggestion i tried to convert all img tag src to linked resource as given below:

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);

When i saw html files then also image was not displaying. I am using htmlagilitypack pack for replacing images.

Can someone please sugegst?

Josh Mein
  • 28,107
  • 15
  • 76
  • 87
manish
  • 61
  • 8
  • What is the output of your testimage.lhtml with simple example of say

    Hello

    .... while I havent used the html agility pack, where are you adding the attachment files to your email
    – BugFinder Jun 11 '18 at 13:05
  • There's more to do than building an html body in which the `src` of the images have been set to a `cid`. Each `LinkedResource` must be added to the `AlternateView` of the email; eg. see the answer on https://stackoverflow.com/questions/50385073/unable-to-send-embedded-image-in-email-using-fluentemail/50564917#50564917 – pfx Jun 11 '18 at 17:52
  • Thanks its working now after adding to alternetview – manish Jun 12 '18 at 12:41

0 Answers0