Catalyst Internet Mail .NET

Extracting Attachments

To determine if a message contains one or more file attachments, the simplest method is to check the value of the Attachment property for each message part. This property returns the name of the file attachment in the current message part or returns an empty string if there is no file attachment. The control checks for standard MIME attachments as well as non-standard uuencoded files which are embedded in the body of the message. The following code demonstrates how this could be done:

bHasAttachments = False

For nPart = 0 To InternetMail1.MessageParts - 1
    InternetMail1.MessagePart = nPart
    If InternetMail1.Attachment.Length > 0 Then
        bHasAttachments = True
        Exit For
    End If
Next

To extract the attached file, use the ExtractFile method and specify the name of the attachment on the local system. For example:

For nPart = 0 To InternetMail1.MessageParts - 1
    InternetMail1.MessagePart = nPart
    If InternetMail1.Attachment.Length > 0 Then
        strFileName = "C:\Temp\" + InternetMail1.Attachment
        If Not InternetMail1.ExtractFile(strFileName) Then
            MsgBox("Unable to extract " + InternetMail1.Attachment)
            Exit For
        End If
    End If
Next

If the file is extracted from the message successfully, the ExtractFile method will return a value of true, otherwise it will return a value of false and the LastError property will be set to an error code which describes the cause of the failure. If the method fails, it would typically mean that the file path is invalid, a file with that name already exists and cannot be overwritten, or the current user does not have the appropriate privileges required to create the file.