Back to product page

Attach Method


Adds new attachment to the message

Syntax

  • Basic
object.Attach (Filename, [ContentType], [Encoding])
The Attach(object,Filename,ContentType,Encoding) syntax has these parts:
objectAn expression evaluating to an object of type SmtpMsg
FilenameRequired. A String value. File on your disk you want to attach to the message.
ContentTypeOptional. A Variant value. Specifies Content-Type header value.
EncodingOptional. A Variant value. Specifies preferred encoding type (base64 is default).

Remarks

Once you want to add attachment to your message, you will use this method. It takes file from your disk, and puts it in appropriate place in the message.

Please note that wodSmtp may do significant changes on your message if you use this method. First, it has to determine if message is already multipart (which is needed to have attachments). If not, it will create appropriate headers (by default this will be 'Content-type: multipart/mixed'), choose boundary to use for parts separation, create new message part, and add encoded attachment to the message.

Using this scenario, it's easy for you to create new message, with a simple line like this:
wodSmtp.Message.Attach "myfile.zip"

Since in above sample we didn't specify encoding to use, wodSmtp uses Base64 as default, and uses Content Type of 'application/octet-stream'.

What happens to the message file after Attach is called? wodSmtp creates temporary file, adds one header 'Content-Transfer-Encoding' with encoding name (base64/binhex/etc..), and after one empty line just writes encoded data. After that, that part is just inserted using Parts.Add method for adding new message parts. We can do this, because Attach generates regular mail message with headers, empty line and some body.

As you might have noticed so far, we didn't specify filename in attachment part. Also, we didn't specify any additional data that can be used by mail client to determine where and how to save this file (except for how to decode it). It would be good idea to add this info manually. This is pretty simple, since Add method returns reference to new part that was just created. Adding new headers is easy:
Dim part As SmtpMsg
 
   Set part = wodSmtp.Message.Attach("c:\myfile.zip")
   part.Headers.Add "Content-Type", "application/x-zip-compressed"
   part.Headers.Add "Content-Disposition", "attachment; filename=""fakefile.zip"""
 

When Add method is used, file is encoded internally and stored to temporary file. wodSmtp will hold this file during encoding, and then it will be deleted.

If you want to add attachment directly from string you can use AttachText method.

Platforms

Windows