Platform SDK: CDO for Windows 2000

BodyPart CoClass

The BodyPart Component Object Model (COM) class defines an object that manages a message body part.

CLSID
Not applicable
ProgID
Not applicable
Type Library
Microsoft CDO for Windows 2000 Library
Inproc Server
CDOSYS.DLL
Threading Model
Both

Implemented Interfaces

IBodyPart

Fields

The following fields comprise the core Multipurpose Internet Mail Extensions (MIME) header fields for body parts.

Name MIME header field name
urn:schemas:mailheader:content-type Content-Type
urn:schemas:mailheader:content-disposition Content-Disposition
urn:schemas:mailheader:content-transfer-encoding Content-Transfer-Encoding
urn:schemas:mailheader:content-id Content-ID
urn:schemas:mailheader:content-base Content-Base
urn:schemas:mailheader:content-location Content-Location
urn:schemas:httpmail:content-disposition-type The part of a Content-Disposition header field that specifies the type (for example, inline or attachment).
urn:schemas:httpmail:content-media-type The part of Content-Type header field that specifies the type (for example, text/plain, text/html, or image/gif).

The list of fields is extensible. You can add other header fields to the body parts by naming the field using the urn:schemas:mailheader: namespace and adding it to the Fields collection.

For example, to include a non-standard header, such as Content-Size, add the following field to the Fields collection: urn:schemas:mailheader:content-size.

Remarks

BodyPart objects make up the hierarchy of content and structural units of a message. Each BodyPart object contains a portion of a Message object's content, such as an attachment. In a MIME-formatted message, BodyPart objects also act as the structural units of the MIME hierarchy. The top-level Message object acts as the root.

Each BodyPart object can have header fields. These fields are contained in the IBodyPart.Fields collection. For objects that make up a MIME-formatted message, the Fields define the MIME headers for the body part. Examples of such header fields are Content-Type and Content-Disposition.

To access the content contained in a BodyPart object, use the IBodyPart.GetDecodedContentStream and IBodyPart.GetEncodedContentStream methods. The encoding format is specified with the urn:schemas:mailheader:content-transfer-encoding field.

Instances of the BodyPart COM class cannot be created directly. They are always returned by instances of the Message COM class for manipulation of a particular body part.

Note

For messages with a single text body part, use the IMessage.Fields collection. Do not use the IBodyPart.Fields collection.

The IBodyPart.DataSource property is not implemented by the BodyPart COM class that the CDO for Windows 2000 COM Library provides. The IBodyPart.GetInterface method can return only the same IBodyPart interface that was used to call the method, and therefore performs no function on instances of the BodyPart object provided by CDO for Windows 2000.

Example

The following code demonstrates how to use BodyPart objects to create a MIME-formatted message. The message is a two-part multipart/mixed message, in which the first part is a multipart/alternative entity and the second part is an image/gif entity. The multipart/alternative entity contains two subparts—one text/html and one text/plain.

[Visual Basic]
Dim iMsg As New CDO.Message
Dim iBp  As CDO.IBodyPart
Dim iBp1 As CDO.IBodyPart
Dim iBp2 As CDO.IBodyPart
Dim Flds As ADODB.Fields
Dim Stm  As ADODB.Stream

Set iBp = iMsg   ' get IBodyPart on Message object

Set Flds = iBp.Fields
Flds("urn:schemas:mailheader:content-type") = "multipart/mixed"
Flds.Update

Set iBp1 = iBp.AddBodyPart  ' Add a BodyPart object to the hierarchy.

Set iBp2 = iBp1.AddBodyPart  ' text/plain
Set Flds = iBp2.Fields
Flds("urn:schemas:mailheader:content-type")              = "text/plain"
Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
Flds.Update

Set Stm = iBp2.GetDecodedContentStream
Stm.WriteText "This is a message"
Stm.Flush

Set iBp2 = iBp1.AddBodyPart   ' text/html
Set Flds = iBp2.Fields
Flds("urn:schemas:mailheader:content-type") = "text/html"
Flds("urn:schemas:mailheader:content-transfer-encoding") = "quoted-printable"
Flds.Update

Set Stm = iBp2.GetDecodedContentStream
Stm.WriteText "<html><h1>This is a message</h1></html>"
Stm.Flush

' This may appear out of order, but we have to _reset_ the 
' content-type for the "multipart/alternative" body part after adding
' the two representations.  If we set it before, it will be switched
' to "multipart/mixed" when we call the AddBodyPart() method on the 
' object.

Set Flds = iBp1.Fields
Flds("urn:schemas:mailheader:content-type") = "multipart/alternative"
Flds.Update

Set iBp1 = iBp.AddBodyPart    '  image/gif
Set Flds = iBp1.Fields
Flds("urn:schemas:mailheader:content-type") = "image/gif"
Flds("urn:schemas:mailheader:content-transfer-encoding") = "base64"

Set Stm = iBp1.GetDecodedContentStream
Stm.LoadFromFile "c:\images\myimage.gif"
Strm.Flush

See Also

IBodyPart Interface