Catalyst Internet Mail .NET

Exporting Messages

The InternetMail class 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 common dialog control to choose a file name to export the current message to:

SaveFileDialog1.DefaultExt = ".txt"
SaveFileDialog1.Title = "Export Message"
SaveFileDialog1.OverwritePrompt = True
SaveFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

If SaveFileDialog1.ShowDialog() <> Windows.Forms.DialogResult.OK Then
    Exit Sub
End If

If Not InternetMail1.ExportMessage(SaveFileDialog1.FileName) Then
    MsgBox("Unable to export message to " + SaveFileDialog1.FileName)
    Exit Sub
End If

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 additional argument which can be used to specify one or more export options:

Constant Description
exportAllHeaders Preserve all headers in the message when it is exported, including those headers which provide routing information such as Received and Return-Path.
exportKeepOrder 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. 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.

A general rule of thumb is that if there is an argument to a method which corresponds to a property and 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.