MailBee. NET Objects Tutorials

Display message with embedded pictures

HTML messages with embedded pictures (and also embedded scripts, CSS tables, and other objects) cannot be rendered without modification of the message body. This is because URLs to embedded objects are represented as CIDs (Content-ID values) rather than real URLs. CID reference in the HTML message body can look as follows: <IMG SRC="cid:picture1"> The above is an example of an image where the image source is the message attachment with Content-ID value equal to "picture1". This image must be displayed in place of <IMG SRC="cid:picture1"> tag when the HTML body is being rendered. Thus, prior to rendering the HTML body of the message, the developer must replace all such pseudo-URLs with real URLs of the corresponding images. For instance, you can save all attachments holding embedded objects to disk and replace corresponding CID references with URLs to the saved attachments. It's, however, quite a complex task to do this manually.

Luckily, MailBee has special features for displaying HTML body with embedded objects. Method GetHtmlAndSaveRelatedFiles saves all embedded objects into a temporary location, replaces all CIDs with their virtual paths, and returns the prepared HTML body string. This method can be used by the web application developer to display an HTML-formatted mail message with embedded pictures in the browser.

When using this method, you should also set WorkingFolder physical location where to save the attachments holding the embedded objects. This physical location must match virtualPath value you pass to GetHtmlAndSaveRelatedFiles method.

C#

// We assume "C:\Inetpub\wwwroot" is a physical path to the location
// visible from Internet as "http://www.domain.com" (virtual path). 
msg.Parser.WorkingFolder = @"C:\Inetpub\wwwroot";
Response.Write(oMsg.GetHtmlAndSaveRelatedFiles("http://www.domain.com", VirtualMappingType.Static, MessageFolderBehavior.DoNotCreate));

VB.NET

' We assume "C:\Inetpub\wwwroot" is a physical path to the location
' visible from Internet as "http://www.domain.com" (virtual path). 
msg.Parser.WorkingFolder = "C:\Inetpub\wwwroot"
Response.Write(oMsg.GetHtmlAndSaveRelatedFiles("http://www.domain.com", VirtualMappingType.Static, MessageFolderBehavior.DoNotCreate))

Thus, if the HTML-formatted message body contains <IMG SRC="cid:pic1"> and the message also has the embedded attachment "picture.gif" with ContentID (CID) "pic1" then calling GetHtmlAndSaveRelatedFiles method will replace "cid:pic1" with http://www.domain.com/picture.gif value.

If you're developing desktop application which does not use virtual paths, you can set virtualPath to NonWeb value. In this case, physical paths to the saved files will be used as URLs.

C#

msg.Parser.WorkingFolder = @"C:\Temp";
Response.Write(msg.GetHtmlAndSaveRelatedFiles(null, VirtualMappingType.NonWeb, MessageFolderBehavior.CreateOnly));

VB.NET

msg.Parser.WorkingFolder = "C:\Temp"
Response.Write(msg.GetHtmlAndSaveRelatedFiles(Nothing, VirtualMappingType.NonWeb, MessageFolderBehavior.CreateOnly))

In other words, embedded pictures will be denoted by their physical paths on the filesystem and these paths will be placed into SRC elements of corresponding HTML tags. For example <IMG SRC="cid:pic1"> will be replaced with <IMG SRC="file:///C:\Temp\picture.gif">).