Displaying HTML-formatted messages (Part 2)

Using temporary filenames for embedded objects Summary: Explains usage of indexed filenames and downloader scripts for accessing embedded 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 topic

Advantages of INDEX path building mode

DIRECT path building mode (PathBuildingMode=1) highlighted in the Part 1 assumes that embedded objects are saved into temporary files under their real filenames. This approach causes problems when the message has two or more embedded objects under the same filename (this happens if these files were taken from different directories during message composing). In this case, all files will be saved under the same filename during GetBodyWithEmbeddedObjectsEx method execution, and each file will overwrite the previous one.

INDEX path building mode (PathBuildingMode=2) forces GetBodyWithEmbeddedObjectsEx method to use number-based filenames (such as "1.gif", "2.jpg", "3.jpg") for the embedded objects. Filename extensions ("gif", "jpg", etc.) remain unchanged while filenames themselves are replaced with numbers. Thus INDEX path building mode is free from the drawback discussed above.

Using SCRIPT path building mode

SCRIPT path building mode (PathBuildingMode=3) allows you to avoid using direct links to the files on your web server. Instead, resulting URL of the embedded object will contain the path to the page capable of downloading files to the users including parameters required to locate the file to download.

This downloader page is called script here, however, it may also be ISAPI DLL, EXE or any other valid path on the server capable of getting dynamic content to the user.

When using SCRIPT path building mode, VirtualPath parameter of GetBodyWithEmbeddedObjectsEx method must contain URL of the downloader script and the string of parameters you want to be passed to it. GetBodyWithEmbeddedObjectsEx method will also add two additional parameters to this URL and use it as a virtual path of the embedded object.

E.g. if:

VirtualPath = "http://www.server.com/email/download_file.asp?user_id=1",

then SRC attribute value of the tag referencing the embedded object will look like:

"http://www.server.com/email/download_file.asp?user_id=1&dir_id=6a7ca92bcce7ad6df6f21ebec50aed8a&file_id=1.gif".

If the URL you pass as a virtual path does not have your own parameters, add "?" to the end of it:

VirtualPath = "http://www.server.com/email/download_file.asp?".

Downloader script requirements

This script must recognize two parameters added by MailBee:

The downloader script must read the file under the following path (in ASP syntax):

FilePath = TempDirectoryPath & "\" & Request("dir_id") & "\" & Request("file_id")

Of course, TempDirectoryPath value used in this path must be equal to that used in GetBodyWithEmbeddedObjectsEx method call.

Finally, the file content must be put in response binary stream.

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

Generic pure-ASP downloader script sample is shown at the end of this page.

Higher security and flexibility through SCRIPT path building mode

Unlike other path building modes, SCRIPT mode does not require embedded object files to be visible from the web. This means you can easily restrict access to these files so they only may be accessed through downloader script. This script may check user permissions, prevent users from peeping other users' files, cancel unauthorized access, gather download statistics, and much more.

The code below prints HTML message to the user, saving all necessary files into physical location "c:\test_project\files" accessible through the downloader script at "http://www.server.com/test/download.asp"


Code example:

' HTML message viewer
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. Embedded
    ' images will be available through "download.asp" script
    Response.Write _
      objMsg.GetBodyWithEmbeddedObjectsEx("c:\test_project\files", _
      "http://www.server.com/test/download.asp?", , 3)
  End If

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

Downloader script sample code below constructs file path assuming "c:\test_project\files" to be a temporary directory path. For simplicity, no user authentication or error checking is performed. Also, in real environments it's preferred to use specialized download components.

It's assumed this script can be accessed at "http://www.server.com/test/download.asp"


Code example:

' Downloader script.

' It takes "dir_id" and "file_id" parameters as input and downloads
' file located in the following directory:
' "c:\test_project\files\" & Request("dir_id") & "\" & Request("file_id")

Dim Msg

' Create MailBee.Message object
Set Msg = Server.CreateObject("MailBee.Message")

' Download file contents
Response.BinaryWrite Msg.GetFileAsArray("c:\test_project\files\" & _
  Request("dir_id") & "\" & Request("file_id"))

See Also:

Part 3 (Removing temporary files after use)

GetBodyWithEmbeddedObjectsEx Method


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