This project is read-only.

Keep inline attachment image links in HTML Mail's

Topics: Feature Requests
Sep 14, 2016 at 7:03 AM
Hello!

Thanks for your awesome library.

I have a feature request concerning the parsing of html emails and inline images.

I want to show the images in a web application and unfortunately, the inline image tags with the ContentId source are removed and replaced by a string like "(Embedded image moved to file: pic23941.gif)".

Is there a flag or another possible solution to keep the image tags as they are in the mail message?

Thanks in advance!

Yours sincerely,
Markus
Sep 18, 2016 at 2:23 AM
I grepped around in the ImapX source code and cannot find any code that replaces img tags with a string like what you provided.

I suspect that what is happening is that you are grabbing the text/plain message body instead of the text/html message body.

That said, a browser control will not render img tags with a cid: source URL. In order to get those img tags to render properly, you'll need to use something like the HtmlPreviewVisitor mentioned at https://github.com/jstedfast/MailKit/blob/master/FAQ.md#HasAttachments (scroll down a few paragraphs).

That example saves the referenced images off to a temp directory and references them using a file:// URL, but the other option is to embed the image data in the HTML like this:
string GetDataImageSrc (MimePart image)
{
    using (var output = new MemoryStream ()) {
        image.ContentObject.DecodeTo (output);
        return string.Format ("data:{0};base64,{1}", image.ContentType.MimeType, Convert.ToBase64String (output.GetBuffer (), 0, (int) output.Length));
    }
}