Partager via


IBodyPart Interface

Topic Last Modified: 2006-06-13

The IBodyPart interface defines a set of methods and properties that you can use to manipulate message body parts.

CLSID

CD000021-8B95-11D1-82DB-00C04FB1625D

Extends

IDispatch

Type Library

Microsoft CDO for Exchange 2000 Library

DLL Implemented In

CDOEX.DLL

Member Summary

The following table lists the properties of the IBodyPart interface.

Name Description

BodyParts

The object's BodyParts collection. This property is read-only.

Charset

The character set for the body part.

ContentClass

Sets and retrieves a body part's content class value.

ContentClassName

Deprecated. Do not use.

ContentMediaType

Contains type and subtype identifiers that specify the nature of the data in a body part.

ContentTransferEncoding

Defines the encoding mechanism used when sending an implementing object's contents over the network.

DataSource

Returns the IDataSource Interface on the object. This property is read-only.

Fields

The Fields collection for the object. This property is read-only.

FileName

The FileName property corresponds to the filename parameter attribute used with the Content-Disposition Multipurpose Internet Mail Extensions (MIME) header field. This property is read-only.

Parent

Returns the parent body part object. This property is read-only.

The following table lists the methods of the IBodyPart interface.

Name Description

AddBodyPart

Adds a BodyPart object to the BodyParts collection.

GetDecodedContentStream

Returns the Microsoft® ActiveX® Data Objects (ADO) Stream object containing the body part content in decoded format.

GetEncodedContentStream

Returns the ADO Stream object containing the body part content in encoded format.

GetFieldParameter

Returns the value of the specified parameter for the specified MIME header field.

GetInterface

Returns the specified dual interface on the object.

GetStream

Returns the ADO Stream object reference containing the headers and content of this body part.

SaveToFile

Saves the decoded contents of a body part to a disk file.

Remarks

A message body that contains more than plain US-ASCII text is subdivided into parts. Examples of body parts include text, attachments, inline HTML pages, alternative representations of HTML pages, and so on. The IBodyPart interface defines a set of abstract methods and properties that you can use to manipulate message body parts.

Body parts are arranged hierarchically in a MIME-formatted message. For example, a message that contains only attachments has a two-level hierarchy the first level includes the message content and the second level contains each attachment. The IBodyPart interface supports such hierarchies by exposing the BodyParts property. Navigating down one level from an implementing object returns a collection of objects, each of which exposes IBodyPart.

Each object that exposes the IBodyPart interface provides access to content in serialized format using the GetDecodedContentStream or GetEncodedContentStream methods. Both methods return an ADO Stream object (exposing an _Stream interface) containing the content in decoded or encoded format, respectively. Content encoding depends upon whether the message body is formatted using MIME or UUENCODE. This behavior is controlled at the Message object level using the MimeFormatted Property.

You can also use the GetStream method to return the entire body part and all subparts in serialized, encoded format. For MIME-formatted messages, this stream contains the mail headers (such as Content-Type) and the content encoded using the mechanism specified by Content-Transfer-Encoding.

For MIME-formatted messages, you must set the Content-Type and Content-Disposition header fields for each body part. Properties on the IBodyPart interface enable you to update these headers quickly. Such properties include the ContentMediaType property, which can be used to set the media-type portion of the Content-Type header. If the object contains a text MIME entity, you can use the Charset property to update the "charset" parameter used with the Content-Type header. Similarly, the ContentTransferEncoding property can be used to update the Content-Transfer-Encoding header for the part. However, all of the header fields are contained in the IBodyPart.Fields collection. You can update any field or add new fields using this collection. The Fields collection returned by the IBodyPart.Fields property is a copy of the fields. To commit changes to the fields, you must call Fields.Update.

Examples

The following example demonstrates manually constructing the MIME structure for a message that contains two alternative representations of a message along with an attachment. This example is for illustrative purposes only. The same message structure could be created using just the IMessage.HTMLBody property and IMessage.AddAttachment method. The example has four steps, as follows:

  • Create a Message object.
  • Add two BodyPart objects to the Message object's BodyParts collection. The first will have content-type set to multipart/alternative and will contain the two representations of the message. The second will contain the attachment.
  • Add two BodyPart objects to the multipart/alternative BodyPart. Each will contain a given representation of the message, in this case "text/plain" and "text/html."
  • Create a third BodyPart object at the first level and add an image (_e2k7.gif) file to it. This body part will contain the attachment for the message.

These steps result in the following hierarchy of MIME entities:

Message (multipart/mixed)
    BodyPart 1 (multipart/alternative)
        BodyPart 1.1 (text/plain)
        BodyPart 1.2 (text/html)
    BodyPart 2 (image/gif)
               (Content-Disposition: attachment)

[Visual Basic]

Dim iMsg As New CDO.Message
Dim iBp1 As CDO.IBodyPart
Dim iBp2 As CDO.IBodyPart
Dim iBp3 As CDO.IBodyPart
Dim Stm As ADODB.Stream

With iMsg
   .From = "Sender@example.com"
   .To = "PersonA@example.com, PersonB@example.com"
   .Subject = "A html and text message with attachment."
End With

''''''''''''''''''''''''''''''''
' Level 1: multipart/mixed (root)
''''''''''''''''''''''''''''''''
Set iBp1 = iMsg         ' returns IBodyPart on object
iBp1.ContentMediaType = "multipart/mixed"


''''''''''''''''''''''''''''''''
' Level 2: multipart/alternative
''''''''''''''''''''''''''''''''
Set iBp2 = iBp1.AddBodyPart
iBp2.ContentMediaType = "multipart/alternative"


''''''''''''''''''''''''''
' Level 3: text/plain
''''''''''''''''''''''''''
Set iBp3 = iBp2.AddBodyPart
With iBp3
   .ContentMediaType = "text/plain"
   .ContentTransferEncoding = "7bit"
   Set Stm = .GetDecodedContentStream
   Stm.WriteText "Here is the plain text version of the message"
   Stm.Flush
End With


''''''''''''''''''''''''''
' Level 3: text/html
''''''''''''''''''''''''''
Set iBp3 = iBp2.AddBodyPart
With iBp3
   .ContentMediaType = "text/html"
   .ContentTransferEncoding = "quoted-printable"
   Set Stm = .GetDecodedContentStream
   Stm.WriteText "<html><body><p>Here is the HTML version of the message</p></body></html>"
   Stm.Flush
End With


'''''''''''''''''''''''''''''
' Level 2: image/gif
'''''''''''''''''''''''''''''
Dim Flds As ADODB.Fields
Set iBp2 = iBp1.AddBodyPart
With iBp2
   .ContentMediaType = "image/gif"
   .ContentTransferEncoding = "base64"
   Set Flds = iBp2.Fields
   Flds("urn:schemas:mailheader:content-disposition") = _
             "attachment; filename=""myimage_e2k7.gif"""
   Flds.Update
   Set Stm = .GetDecodedContentStream
   Stm.LoadFromFile "c:\somewhere\myimage_e2k7.gif"
   Stm.Flush
End With

' Clean up.
Stm.Close
Set Stm = Nothing
Debug.Print iMsg.GetStream.ReadText

[VBScript]

Dim iMsg
Dim iBp1
Dim iBp2
Dim iBp3
Dim Stm

Set iMsg = CreateObject("CDO.Message")

With iMsg
   .From    = "Sender@example.com"
   .To      = "PersonA@example.com, PersonB@example.com"
   .Subject = "A html and text message with attachment."
End With

''''''''''''''''''''''''''''''''
' Level 1: multipart/mixed (root)
''''''''''''''''''''''''''''''''
Set iBp1 = iMsg.BodyPart
iBp1.ContentMediaType = "multipart/mixed"


''''''''''''''''''''''''''''''''
' Level 2: multipart/alternative
''''''''''''''''''''''''''''''''
Set iBp2 = iBp1.AddBodyPart
iBp2.ContentMediaType = "multipart/alternative"


''''''''''''''''''''''''''
' Level 3: text/plain
''''''''''''''''''''''''''
Set iBp3 = iBp2.AddBodyPart
With iBp3
   .ContentMediaType        = "text/plain"
   .ContentTransferEncoding = "7bit"
   Set Stm = .GetDecodedContentStream
   Stm.WriteText "Here is the plain text version of the message"
   Stm.Flush
End With


''''''''''''''''''''''''''
' Level 3: text/html
''''''''''''''''''''''''''
Set iBp3 = iBp2.AddBodyPart
With iBp3
   .ContentMediaType        = "text/html"
   .ContentTransferEncoding = "quoted-printable"
   Set Stm = .GetDecodedContentStream
   Stm.WriteText "<html><body><p>Here is the HTML version of the message</p></body></html>"
   Stm.Flush
End With


'''''''''''''''''''''''''''''
' Level 2: image/gif
'''''''''''''''''''''''''''''
Dim Flds
Set iBp2 = iBp1.AddBodyPart
With iBp2
   .ContentMediaType = "image/gif"
   .ContentTransferEncoding = "base64"
   Set Flds = iBp2.Fields
   Flds("urn:schemas:mailheader:content-disposition") = "attachment; filename=""myimage_e2k7.gif"""
   Flds.Update
   Set Stm = .GetDecodedContentStream
   Stm.LoadFromFile "c:\somewhere\myimage_e2k7.gif"
   Stm.Flush
End With

' Clean up.
Stm.Close
Set Stm = Nothing
Wscript.Echo iMsg.GetStream.ReadText