File Attachments | ||||||||||
In addition to sending text messages, email is commonly used as a means to exchange files. This can be easily done using the Mail Message control's 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 Len(editFileName.Text) > 0 Then nError = MailMessage1.AttachFile(editFileName.Text) If nError Then MsgBox "Unable to attach file " & editFileName.Text & _ vbCrLf & MailMessage1.LastErrorString, vbExclamation Exit Sub End If If the file does not exist or cannot be accessed, then the AttachFile method will return an error. Otherwise, the file data will be encoded and attached to the message. The AttachFile method has two arguments, the name of the file to attach and an optional argument which specifies how the attachment should be encoded. In most cases, it is not necessary to specify the optional argument because 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:
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 Len(editFileName.Text) > 0 Then nError = MailMessage1.AttachFile(editFileName.Text, mimeAttachBase64) If nError Then MsgBox "Unable to attach file " & editFileName.Text & _ vbCrLf & MailMessage1.LastErrorString, vbExclamation Exit Sub 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, displaying 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 you attach files to plain text messages. |
||||||||||
Copyright © 2024 Catalyst Development Corporation. All rights reserved. |