Displaying HTML-formatted messages (Part 1)

Quickly display e-mail with inline pictures Summary: Discusses displaying HTML-formatted e-mail messages using ASP, including handling embedded (inline) objects.

Tutorial map:

Part 1 - Quickly display e-mail with inline pictures
Part 2 - Using temporary filenames for embedded objects
Part 3 - Removing temporary files after use
Part 4 - Advanced topics

HTML messages often contain embedded pictures, sounds, scripts, style-sheets, etc. Usually HTML message refers them by URLs but quite often these objects are just attached to the message (they called inline objects). In order to correctly render HTML content, these objects must be saved to disk first.

GetBodyWithEmbeddedObjectsEx method of MailBee.Message object saves the message's embedded objects into temporary location (by default, the current user's temporary folder) and modifies URLs of these objects in HTML source to match their new locations. GetBodyWithEmbeddedObjectsEx returns modified HTML body text, original Message.BodyText property value is not affected.

GetBodyWithEmbeddedObjectsEx method takes into account the fact that files are saved under physical path (e.g. "c:\inetpub\wwwroot\test\data"), but correponding URL looks like "http://www.server.com/test/data", i.e. some physical path must be mapped to corresponding virtual path in order to be visible to the clients accessing the site.

GetBodyWithEmbeddedObjectsEx method supports several physical-to-virtual path mappings. DIRECT mode is the simplest case: some physical root is mapped to corresponding virtual root, and physical paths relative to physical root directly repeat virtual paths relative to virtual root. I.e. "c:\inetpub\wwwroot\test\data\dir1\picture.gif" is mapped into "http://www.server.com/test/data/dir1/picture.gif".

TempDirectoryPath parameter sets physical path to the location where temporary files will be stored.

VirtualPath parameter is a virtual path to the location specified by TempDirectoryPath physical path.

PathBuildingMode parameter sets physical-to-virtual mapping mode. To enable DIRECT mode, set PathBuildingMode=1.

Rest of parameters are not important for now, so leave them with the default values.

Note: TempDirectoryPath folder must have full-control permissions for the IIS account MailBee is running on (usually IUSR_<YOUR_SERVER_NAME>).

The code below prints HTML message to the user, saving all necessary files into physical location "c:\test_project\files" accessible from the web as "http://www.server.com/test/files"


Code example:

Dim objPOP3, objMsg
Set objPOP3 = Server.CreateObject("MailBee.POP3")
objPOP3.LicenseKey = "put your license key here"

' Connect to POP3 server
If objPOP3.Connect("mail.server.com", 110, "user", "pass") Then

  ' Mailbox not empty?
  If objPOP3.MessageCount > 0 Then

    ' Get a message
    Set objMsg = objPOP3.RetrieveSingleMessage(1)

    ' Save embedded images and display message body
    Response.Write _
      objMsg.GetBodyWithEmbeddedObjectsEx("c:\test_project\files", _
      "http://www.server.com/test/files", , 1)
  End If

  objPOP3.Disconnect
Else
  Response.Write objPOP3.ErrDesc
End If

See Also:

Part 2 (Using temporary filenames for embedded objects)

GetBodyWithEmbeddedObjectsEx Method


Copyright © 2002-2022, AfterLogic Corporation. All rights reserved.