Sdílet prostřednictvím


AddBodyPart Method

Topic Last Modified: 2006-06-13

Adds a BodyPart object to the BodyParts collection.

Applies To

IBodyPart Interface

Type Library

Microsoft CDO for Exchange 2000 Library

DLL Implemented In

CDOEX.DLL

Syntax

Function AddBodyPart(    ByVal [Index As Long]) As IBodyPart
HRESULT AddBodyPart
(
        long Index,
        IBodyPart** pVal
);

Parameters

  • Index
    The ordinal index within the body parts collection at which the new body part object is to be added. The first element in the collection has an index of 1, and the current last element has an index equal to the value of the collection's Count property. Setting the Index to 1 causes the new body part to be inserted at the beginning of the collection. Setting the Index to -1 causes the new body part to be added (appended) to the end of the collection. Index settings of 0 or less than -1 are invalid.

Return Value

Returns S_OK if successful, or an error value otherwise.

Remarks

The AddBodyPart method creates a new BodyPart object in the BodyParts collection and then returns the IBodyPart interface on the new object. This method is essentially the same as using the Add Method on this object's BodyParts collection.

If an object already exists in the collection at the point you specify with the Index parameter, the new object is added in its place, and the index values of the existing object, and all following objects, are increased by one.

The Multipurpose Internet Mail Extensions (MIME) specification requires that a multipart item must hold all its content in subparts and not at its own level. Therefore, if AddBodyPart is called on a message that is not multipart and that holds content in its HTMLBody Property or TextBody Property, this content is converted to one or more subparts, which become the first elements of the new body parts collection created by AddBodyPart. The new object returned by AddBodyPart is added as the last element in this new collection.

When calling AddBodyPart, the ContentMediaType property is checked for the value "multipart". If not found, ContentMediaType is changed to "multipart/mixed" and a new BodyParts collection is created on the body part to which this body part is being added. If ContentMediaType was already "multipart/alternative", it is changed to "multipart/mixed", and the entire multipart/alternative structure is made a subpart of the body part to which you are adding the new body part. This is necessary because the new body part, being empty, cannot possibly be an alternative rendition of the other subparts of the multipart/alternative structure.

The ContentMediaType of the new body part is set to "application/octet-stream". This prevents the IMessage.HTMLBodyPart or IMessage.TextBodyPart property from mistaking the new body part for HTML or plain text in case it is not one of these types. You should set ContentMediaType to the appropriate value when you obtain the body part from the AddBodyPart method.

Body parts that will contain attachments can also be added directly via the AddAttachment Method. For simplicity it is recommended that you use the AddAttachment method to associate an attachment with the message.

Example


Dim Flds As ADODB.Fields
Dim Strm As ADODB.Stream

Dim iMsg As New CDO.Message
Dim iBp As CDO.BodyPart

Set iBp = Msg.BodyPart.AddBodyPart
Set Flds = iBp.Fields
Flds("urn:schemas:mailheader:content-type") = "text/plain"
Flds.Update
Set Strm = iBp.GetDecodedContentStream
Strm.WriteText "This is some text"
Strm.Flush

Set iBp = Msg.BodyPart.AddBodyPart
Set Flds = iBp.Fields
Flds("urn:schemas:mailheader:content-type") = "text/html"
Flds.Update
Set Strm = iBp.GetDecodedContentStream
Strm.WriteText "<html><h1>This is some text</h1></html>"
Strm.Flush

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

' Clean up.
Strm.Close
Set Strm = Nothing