Displaying HTML-formatted messages (Part 2)

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 - Removing temporary files after use
Part 3 - 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 GetBodyWithEmbeddedObjects 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 current user's temporary directory. So, each message holds its temporary files in its own directory (called message directory), and all these directories are located in the current user's temporary directory.

To perform files clean-up it's enough to call RemoveMessageDirectory method of the Message object.

RemoveMessageDirectory method gets message directory path stored in the Message object by GetBodyWithEmbeddedObjectsEx method call, and deletes the folder under this path. The folder content is also removed. If the message had no embedded objects, it's still safe to call this method. It just does nothing in such a case.

Note: It's important to understand that RemoveMessageDirectory method must be called for THE SAME Message object instance that was previously used for GetBodyWithEmbeddedObjectsEx method call.

For this purpose, the sample code below declares the Message object as global (not local, within a subroutine or a function) to keep it alive until the user has finished viewing the message.

The sample below displays the message on Command1_Click event and cleans-up on Form_Unload. The Message object is a global variable, so Form_Unload sub operates with the same instance of the Message object as that one previously set in Command1_Click.


Code example:

' Assume objWebBrowser is a valid WebBrowser control
' (It's included in Microsoft Internet Controls type library).

' This sample is for VB only, ASP samples are discussed in another tutorial

Dim objPOP3, objMsg

Private Sub Command1_Click()
  Set objMsg = Nothing
  Set objPOP3 = 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)
      objWebBrowser.Document.Write objMsg.GetBodyWithEmbeddedObjectsEx(, , , 4, , )
    End If
    objPOP3.Disconnect
  Else
    MsgBox objPOP3.ErrDesc
  End If
End Sub

Private Sub Form_Load()
  Set objMsg = Nothing
  objWebBrowser.Navigate "about:blank"
End Sub

Private Sub Form_Unload(Cancel As Integer)
  If Not objMsg Is Nothing Then
    objMsg.RemoveMessageDirectory
  End If
End Sub

See Also:

Part 3 (Advanced topics)

GetBodyWithEmbeddedObjectsEx Method
RemoveMessageDirectory Method


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