facebooktwittermenuarrow-up

GemBox Support Forum

Issue with embedded images in an email

I have been using:

    private string ReplaceEmbeddedImages(string htmlBody, AttachmentCollection attachments)
    {
        var srcPattern =
            "(?<=<img.+?src=[\"'])" +
            "(.+?)" +
            "(?=[\"'].*?>)";

        // Iterate through the "src" attributes from HTML images in reverse order.
        foreach (var match in Regex.Matches(htmlBody, srcPattern, RegexOptions.IgnoreCase).Cast<Match>().Reverse())
        {
            var imageId = match.Value.Replace("cid:", "");
            Attachment attachment = attachments.FirstOrDefault(a => a.ContentId == imageId);

            if (attachment != null)
            {
                // Create inlined image data. E.g. "data:image/png;base64,AABBCC..."
                ContentEntity entity = attachment.MimeEntity;
                var embeddedImage = entity.Charset.GetString(entity.Content);
                var embeddedSrc = $"data:{entity.ContentType};{entity.TransferEncoding},{embeddedImage}";

                // Replace the "src" attribute with the inlined image.
                htmlBody = $"{htmlBody.Substring(0, match.Index)}{embeddedSrc}{htmlBody.Substring(match.Index + match.Length)}";
            }
        }

        return htmlBody;
    }

which is called by LoadBody for html emails. I have encountered some eml files that fail that contain the following <img line structure:

<img alt=3D"[image]" src=3D"cid:XYZ_LOGO.png">

in this case after line:

ContentEntity entity = attachment.MimeEntity;

entity.Content is null so I have modified the code as follows:

    private string ReplaceEmbeddedImages(string htmlBody, AttachmentCollection attachments)
    {
        var srcPattern =
            "(?<=<img.+?src=[\"'])" +
            "(.+?)" +
            "(?=[\"'].*?>)";

        // Iterate through the "src" attributes from HTML images in reverse order.
        foreach (var match in Regex.Matches(htmlBody, srcPattern, RegexOptions.IgnoreCase).Cast<Match>().Reverse())
        {
            var imageId = match.Value.Replace("cid:", "");
            Attachment attachment = attachments.FirstOrDefault(a => a.ContentId == imageId);

            if (attachment != null)
            {
                // Create inlined image data. E.g. "data:image/png;base64,AABBCC..."
                ContentEntity entity = attachment.MimeEntity;
                var embeddedImage = imageId;
                if (entity.Content != null)
                {
                    embeddedImage = entity.Charset.GetString(entity.Content);
                }

                var embeddedSrc = $"data:{entity.ContentType};{entity.TransferEncoding},{embeddedImage}";

                // Replace the "src" attribute with the inlined image.
                htmlBody = $"{htmlBody.Substring(0, match.Index)}{embeddedSrc}{htmlBody.Substring(match.Index + match.Length)}";
            }
        }

        return htmlBody;
    }

which seem to work I wanted to make sure that this will cause no other side effects. Thanks in advance

Tim H

Hi Timothy,

Can you send us a sample of the email that has entity.Content as null?

I’ll modify our example code to something like this:

if (attachment != null)
{
    // Create inlined image data. E.g. "data:image/png;base64,AABBCC..."
    ContentEntity entity = attachment.MimeEntity;
    if (entity.Content != null)
    {
        var embeddedImage = entity.Charset.GetString(entity.Content);
        var embeddedSrc = $"data:{entity.ContentType};{entity.TransferEncoding},{embeddedImage}";

        // Replace the "src" attribute with the inlined image.
        htmlBody = $"{htmlBody.Substring(0, match.Index)}{embeddedSrc}{htmlBody.Substring(match.Index + match.Length)}";
    }
}

Regards,
Mario