Catalyst Internet Mail .NET

File Attachments

In addition to sending text messages, e-mail is commonly used as a means to exchange files. This can be easily done using the AttachFile method. Let's modify the previous example, presuming that an edit control has been included on the form which allows a user to input the name of the file they wish to attach. Add this after the call to the ComposeMessage method:

' If a file name has been entered, then attach it to
' the message that was composed
If editFileName.Text.Length > 0 Then
    If Not InternetMail1.AttachFile(editFileName.Text) Then
        MessageBox.Show(InternetMail1.LastErrorString, "Error")
        Exit Sub
    End If
End If

If the file does not exist or cannot be accessed, then the AttachFile method will return a value of false, indicating an error. Otherwise, the file data will be encoded and attached to the message. The AttachFile method has two implementations, one that simply specifies the name of the file and another that specifies both the name of the file to attach and an argument which can be used to specify how the attachment is encoded.

In most cases, it is not necessary to specify the optional argument since the AttachFile method will automatically determine the correct encoding method based on the contents of the file. However, there are some situations in which you may wish to use some specific encoding method. For example, you may want to force the control to use base64 encoding even though the attachment is a plain text file. To do this, you can use one of the following values:

Attachment Options

MimeAttachment Enumeration Description Value
attachDefault The file attachment encoding is based on the file content type. Text files are not encoded, and binary files are encoded using the standard base64 encoding algorithm. This is the default option for file attachments. 0
attachBase64 The file attachment is always encoded using the standard base64 algorithm, even if the attached file is a plain text file. 1
attachUucode The file attachment is always encoded using the uuencode algorithm, even if the attached file is a plain text file. 2
attachQuoted The file attachment is always encoded using the quoted-printable algorithm, even if the attached file is a plain text file. 3
attachAlternative The attached data is an alternative format for the contents of the message. This can only be used with textual data. 65536
attachInline The attached data is to be displayed inline with the contents of the message. This is typically used with images that are to be displayed along with the message text. 131072

For example, if you want to always have the attached file encoded using the base64 algorithm, the code would be changed to look like this:

' If a file name has been entered, then attach it to
' the message that was composed
If editFileName.Text.Length > 0 Then
    If Not InternetMail1.AttachFile(editFileName.Text, MimeAttachment.attachBase64) Then
        MessageBox.Show(InternetMail1.LastErrorString, "Error")
        Exit Sub
    End If
End If

When attaching a file, keep in mind that the size of the attachment in the message will typically be about 33% larger than the size of the file itself. This is an important consideration because most mail servers restrict the size of the messages they will accept and will reject messages that exceed that limit. For example, if a mail server restricts messages to 5 megabytes, the maximum size of a file that can be attached to the message is about 3.5 megabytes.

Another consideration with file attachments is compatibility with third-party mail client software. If the current message contains alternative messages (i.e., both plain text and HTML text) then AttachFile will change the message structure, creating a more complex multipart message which has mixed content types. Mail software which does not fully conform to the MIME standard may not be able to correctly display this type of message, either being unable to display the body of the message or display the complete message including the alternate text and the encoded file attachment. To ensure that your message is readable by most recipients, it's recommended that files are attached to plain text messages.