Exporting Messages

The Internet Mail control has the ability to export the current message contents as a string or as a text file on the local system. When a message is exported, the complete message including headers, message body and any file attachments are included. The following example uses the CommonDialog control to choose a file name to export the current message to:

Dim nError As Long

On Error GoTo ExportCanceled
CommonDialog1.CancelError = True
CommonDialog1.DefaultExt = ".txt"
CommonDialog1.DialogTitle = "Export Message"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNLongNames + cdlOFNOverwritePrompt
CommonDialog1.Filter = "Text Files (*.txt)|*.txt|" & _
                       "E-Mail Message Files (*.eml)|*.eml|" & _
                       "All Files (*.*)|*.*"

CommonDialog1.ShowSave
On Error GoTo 0

nError = InternetMail1.ExportMessage(CommonDialog1.FileName)
If nError Then
    MsgBox "Unable to export message to " & _
           CommonDialog1.FileTitle & vbCrLf & _
           InternetMail1.LastErrorString, vbExclamation
    Exit Sub
End If

MsgBox "Exported message to " & CommonDialog1.FileName & vbCrLf & _
       "regarding " & Chr(34) & InternetMail1.Subject & Chr(34), _
       vbInformation

ExportCanceled:
Exit Sub

When a message is exported, headers may be re-ordered and certain headers which contain routing information (such as Received and Return-Path) are omitted by default. These headers are not normally needed when composing or delivering a message, however, there may be situations in which an application needs to preserve these headers or the order in which they were originally received. The ExportMessage method has an optional argument which can be used to specify one or more export options:

Constant Description
mailOptionAllHeaders Preserve all headers in the message when it is exported, including those headers which provide routing information such as Received and Return-Path.
mailOptionKeepOrder Preserve the original order of the message headers. This is only useful if the message was retrieved from a mail server or imported from a file.

These two values may be combined if both options are required. For example, the following code would export a message with all of the headers, preserving their original order:

nOptions = mailOptionAllHeaders Or mailOptionKeepOrder
nError = InternetMail1.ExportMessage(strFileName, nOptions)

Note that the option values are actually bit flags, so a bitwise Or operation is used to combine them. This method is preferred over using simple addition which can produce unexpected results in some cases. Also note that if the ExportMessage method is called without specifying the optional argument, then the value of the Options property is used as a default, such as:

InternetMail1.Options = mailOptionAllHeaders Or mailOptionKeepOrder
nError = InternetMail1.ExportMessage(strFileName)

This would also cause the message to be exported with all headers and preserve their original order. A general rule of thumb is that if there is an optional argument to a method which corresponds to a property in the control, if that argument is not specified, the property value will be used as a default.

If an application needs to do some processing on the message but doesn't want the overhead of exporting the message to a file, then the message contents can be read using the control's Message property. This property returns a string which contains the complete message, including all headers. The Options property determines whether or not all headers are exported and if the original header order is preserved, just as with the ExportMessage method. It should be noted that this is different than the MessageText property, which returns only the body of the current message part, not the complete message.


Copyright © 2008 Catalyst Development Corporation. All rights reserved.