0 votes

I fetch mails via imap. The server returns some mails with embedded images with the src attributes as <img src="cid:filename.jpg">. When i render this mail as html, the images appears broken.

Please note: I have downloaded the Visuals and saved it to a folder. Could this be the reason? Did i lose the files after downloading the visuals?

by (551 points)

1 Answer

0 votes

In most cases HTML body of the message references attached images using special "cid:" protocol. It specifies the Content-ID of the image, not the name of the file:

This is our <strong>brand new</strong> logo: <br />
<img src="cid:logo@example.com" />

The actual image is embedded inside an email, as a part of a MIME tree, as an element related to the HTML body and with content-disposition header set to inline.

You should use IMail.SaveHtmlAs method. It saves HTML version of the message body as regular HTML file (changing cid: references). All visual elements are saved as files in the same folder.

If the message is plain text only, this method uses GetBodyAsHtml() to create HTML from plain text.

by (279k points)
Actually, i want all mails as html including text as html. so i used string body = email.GetBodyAsHtml();

Mail.SaveHtmlAs saves it as a file in a folder. i dont want that. i want it as string with proper referencing to images sources whether locally or remotely hosted images without the cid: prefixing.
cid protocol is the only proper image reference. Image is not stored on disk but inside the email message (inside a MIME tree).
When you access the Mime tree of the email message and save the visuals in a folder, does it affect the MIME Tree thereby breaking the cid: references to the Visuals. In my code below, i accessed the mime tree and saved the Visuals in a folder. Could this be the reason why cid: references fails. I'm currently having the thought to delete the code that extracts visuals

foreach (MimeData mime in email.Visuals)
   string guidattach = Guid.NewGuid().ToString() + "☻" + mime.SafeFileName;
   string newfilename = attachmentfolder + guidattach;
   string virtualfilename =    "~\\other_attach\\externalmail_attachment\\" + guidattach;
   pagesb.Append(virtualfilename + ">");
Saving MimeData element doesn't affect the MIME tree in any way.

I think your problem is elsewhere:
If you save the HTML and visuals to a single folder, browser won't be able to use cid references. Files on disk don't have content-id assigned. Cid references are useless in such case. Cid have a meaning in terms of the MIME document.

You need to modify html to point to actual files on disk, and this is what IMail.SaveAsHtml method does.