Displaying HTML-formatted messages (Part 3)

Removing temporary files after use Summary: Shows how to remove temporarily saved 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 topics

When the user has finished viewing the message containing embedded objects, it's a time to delete all temporary files because they are no longer needed.

When GetBodyWithEmbeddedObjectsEx method of the Message object is called and embedded objects are present in the message, this Message object remembers the path to the saved temporary files. These files are stored in the special directory created by GetBodyWithEmbeddedObjectsEx method in the TempDirectoryPath directory. So, each message holds its temporary files in its own directory (called message directory), and all these directories are located in TempDirectoryPath directory.

Once GetBodyWithEmbeddedObjectsEx method was called, you may get the path to the message directory by obtaining value returned by GetMessageDirectoryPath method. Store this value in the Session, you will need it later.

Some time later the user clicks "Logout" button (or the Session expires due the user's inactivity), and clean-up script is executed. To delete all temporary data, this script must delete the message directory including all its contents.

To do this, call RemoveMessageDirectory method of the Message object, passing the message directory path stored in the Session as a value of MessageDirectoryPath parameter.

Note: Many (even most) emails do not contain embedded objects, and the message directory is not created for such messages. GetMessageDirectoryPath method returns empty string in this case. However, it's safe to call RemoveMessageDirectory method passing empty string as a parameter. It will just do nothing.

The sample below adds the message directory path storing to the sample from Part 2. The path is stored in the session variable named "strMessageDir".


Code example:

' HTML message viewer
Dim objPOP3, objMsg
Set objPOP3 = Server.CreateObject("MailBee.POP3")
objPOP3.LicenseKey = "put your license key here"
If objPOP3.Connect("mail.server.com", 110, "user", "pass") Then
  If objPOP3.MessageCount > 0 Then
    Set objMsg = objPOP3.RetrieveSingleMessage(1)
    Response.Write _
      objMsg.GetBodyWithEmbeddedObjectsEx("c:\test_project\files", _
      "http://www.server.com/test/download.asp?", , 3)
    Session("strMessageDir") = objMsg.GetTemporaryDirectoryPath
  End If
  objPOP3.Disconnect
Else
  Response.Write objPOP3.ErrDesc
End If

Sample code below is a script that should be called whenever clean-up takes place (logout and session expiration are common examples).

The script assumes the message directory path is stored in "strMessageDir" session variable.


Code example:

' Temporary files remover
Dim objMsg
Set objMsg = Server.CreateObject("MailBee.Message")
objMsg.RemoveMessageDirectory Session("strMessageDir")

See Also:

Part 4 (Advanced topics)

GetBodyWithEmbeddedObjectsEx Method
RemoveMessageDirectory Method
GetMessageDirectoryPath Method


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