I will provide some basic information first. There are two common ways of including resources such as pictures in HTML e-mails.
The first way involves adding the "Content-Location" header to the HTML part and to all images to set their URL. The second way is to add the "Content-ID" to all images to assign a unique ID for each of them.
In the first case, the HTML or CSS references the images as if they were placed at the specified URL, so it is possible to write <img src="image.jpg">
in HTML or background: url(image.jpg)
in CSS. This is very easy, but a major drawback is that MS Outlook is unable to display these e-mails correctly (unlike MS Outlook Express, Mozilla Thunderbird and others).
The second way is more compatible and thus safer. In this case, the images has to be referred using their content ID prefixed by "cid:", so the HTML uses <img src="cid:1234@domain">
and CSS uses background: url(cid:1234@domain)
. Outlook is able to display this without problems, and it seems to be used more often.
If you send content of either of these two HTML e-mails to the browser, no images will be displayed. <img src="image.jpg">
won't show anything because image.jpg
doesn't exist at the server, and <img src="cid:1234@domain">
isn't even a proper link. The solution is to process the HTML first.
- Replace these links with links to a dedicated page at the server - for
example, transform
<img src="image.jpg">
into
<img src="Image.aspx?name=image.jpg">
and
<img src="cid:1234@domain">
into
<img src="Image.aspx?cid=1234@domain">
- Create a page (called
Image.aspx
for
example) that either takes an image
name or CID as input and sends the
corresponding attachment data (and
content type) to the browser. You
will probably also have to pass some
kind of message ID to that page.
Obviously, this poses some problems:
- You probably don't want to download the same message from the IMAP server again and again for each image, so it might be necessary to implement a cache of some kind for downloaded messages or their attachments/embedded images.
- HTML processing also is quite complicated - you either need a smart regular expression, or a HTML parser. Fortunately, there are some freely available parsers in .NET available on the web.
Actually, we originally wanted to implement an IMAP/POP3 web mail client as a sample, but after considering all these difficulties (and more, including embedded CSS and embedded images referenced from CSS), we postponed this indefinitely. It is definitely not an easy task, and would actually deserve a component of its own.