Attachments Property

Attachments Property

The Attachments property is the collection of message attachments.


Property Attachments as IBodyParts
HRESULT get_Attachments(IBodyParts** pVal);


Attachments are stored as body parts in the body part hierarchy of the message. The Attachments property returns a collection that contains BodyPart objects, each of which contains an attachment to the message. In most cases, attachments are flagged as attachments in messages formatted in Multipurpose Internet Mail Extensions (MIME) by setting the MIME Content-Disposition header to attachment; however, a given body parts status as an attachment is implementation dependent.

The Attachments collection, although modeled using the IBodyParts interface (exposed by the returned BodyParts object) is not the same as the collection returned by the IBodyPart.BodyParts collection for the object exposing the IMessage interface. When you request the IMessage.Attachments collection, the object returns a collection of only those BodyPart objects that should contain an attachment to the message; for example, consider the following MIME hierarchy for a message:

1. Content-Type: multipart/mixed
2.    Content-Type: multipart/alternative
3.       Content-Type: text/plain
           (text content here)
4.       Content-Type: text/html
           (html content here)
5.    Content-Type:        application/octet-stream
      Content-Disposition: attachment
        (content here)
6.    Content-Type:        application/msword
      Content-Disposition: attachment
        (content here)

In this case, the IMessage.Attachments property returns a BodyParts collection that contains two BodyPart objects: one for body part 5 and one for body part 6, as they are defined in the previous example. The IBodyPart.BodyParts property returns a BodyParts collection containing three BodyPart objects: one for body part 2, one for body part 5, and one for body part 6.

When you add body parts using the IBodyParts interface passed by the Attachments property, the Content-Disposition header (urn:schemas:mailheader:content-disposition) is automatically set to attachment.


This code determines how many attachments a message has:

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Sub GetAttachmentCount( iMsg as CDO.Message )
  Dim collAtts as CDO.IBodyParts
  Set collAtts = iMsg.Attachments
  If collAtts.Count = 0 Then
    MsgBox "Attachments collection is empty"
    MsgBox "Message has " & collAtts.Count & " attachments"
  End If
End Sub

See Also


IBodyParts Interface