Importing Messages  
 

In addition to using the ComposeMessage method to create a new message, it is possible to import existing messages into the Mail Message control. These messages may exist either as text files already stored on the local system, records in a database, or may even be created dynamically by the application using data from other sources. The most important thing to keep in mind is that any message which is imported into the control must adhere to the basic structure outlined previously in the section discussing message composition. The control is tolerant of malformed messages, however, importing a corrupted message will often produce unexpected results. If the source of the message is unknown, it is recommended that an application perform checks to ensure that it contains reasonable values. For example, check to make sure the From and To header fields contain email addresses, the message has a valid Date, and a message body is present.

The simplest method of importing a message into the control is using the ImportMessage method. Here is an example which uses the Common Dialog control to select a file and then calls ImportMessage to import the file:

Dim nError As Long
    
On Error GoTo ImportCanceled
CommonDialog1.CancelError = True
CommonDialog1.DefaultExt = ".txt"
CommonDialog1.DialogTitle = "Import Message"
CommonDialog1.FilterIndex = 1
CommonDialog1.Flags = cdlOFNFileMustExist + cdlOFNLongNames
CommonDialog1.Filter = "Text Files (*.txt)|*.txt|" & _
                       "email Message Files (*.eml)|*.eml|" & _
                       "All Files (*.*)|*.*"
    
CommonDialog1.ShowOpen
On Error GoTo 0
    
nError = MailMessage1.ImportMessage(CommonDialog1.FileName)
If nError Then
    MsgBox "Unable to import message from " & _
           CommonDialog1.FileTitle & vbCrLf & _
           MailMessage1.LastErrorString, vbExclamation
    Exit Sub
End If

MsgBox "Imported message from " & MailMessage1.From & vbCrLf & _
       "regarding " & Chr(34) & MailMessage1.Subject & Chr(34), _
       vbInformation

Exit Sub

ImportCanceled:
Exit Sub

Once the message has been imported successfully, the various message related properties can be accessed just as if the message had been composed. Note that the current message, if any, will be completely replaced by the message that has been imported.

Another method of importing a message is from a string. This is useful if a message has been stored in something other than a text file such as a record in a database. To do this, simply set the control's Message property to the string which contains the message:

On Error Resume Next: Err.Clear
MailMessage1.Message = strMessage

If Err.Number Then
    MsgBox Err.Description, vbExclamation
    Exit Sub
End If

Unlike the ImportMessage method, which returns an error code if it fails, setting the Message property will result in an error being raised if there is a problem. Because of this, any application which sets the Message property should use an error handler. In this example, it simply executes the next statement and uses the Err object to obtain the error code and description.

A third method of importing a message into the control is to use the ParseMessage method. Unlike the ImportMessage method or the Message property, it is not required that the complete message be available at once. Instead, ParseMessage enables an application to import a message in pieces, dynamically parsing the data and adding to the contents of the current message. The following example opens a file which contains a message, reads it in 1,024 byte blocks and then passes it to the ParseMessage method:

hFile = FreeFile()
Open strFileName For Input As hFile
nFileLength = LOF(hFile)

MailMessage1.ClearMessage
    
Do While nFileLength > 0
    cbBuffer = nFileLength
    If cbBuffer > 1024 Then cbBuffer = 1024
    nFileLength = nFileLength - cbBuffer
    strBuffer = Input(cbBuffer, hFile)
    nError = MailMessage1.ParseMessage(strBuffer)
    If nError > 0 Then
        MsgBox MailMessage1.LastErrorString, vbExclamation
        Exit Do
    End If
Loop
    
Close hFile

The initial call to the ClearMessage method ensures that if there is a current message, the contents are cleared first. Then the ParseMessage method is repeatedly called until the end-of-file is reached. This approach could be used when a message is being created by some external data source or the application needs to make some sort of change to the message contents dynamically. Note that the purpose of the above example is to demonstrate how to use the ParseMessage method and is not the recommended procedure for importing a message from a text file. Refer to the technical reference documentation for the ImportMessage method for more information on importing messages from text files.