Compartir a través de


AddBodyPart Method

AddBodyPart Method

The AddBodyPart method adds a BodyPart object to the BodyParts collection.

Syntax

Function AddBodyPart([ByVal Index as Long = -1]) as IBodyPartHRESULT AddBodyPart( long Index = -1, IBodyPart** pVal);

Parameters

  • Index
    The ordinal index within the BodyParts collection at which the new body part object is 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.

Remarks

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

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 value 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 the AddBodyPart method is called on a message that is not multipart and that holds content in its IMessage.HTMLBody or IMessage.TextBody properties, this content is converted to one or more subparts, which become the first elements of the new body parts collection created by the AddBodyPart method. The new object returned by the AddBodyPart method is added as the last element in this new collection.

When calling the AddBodyPart method, the ContentMediaType property is checked for the value multipart. If not found, the ContentMediaType property 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 the ContentMediaType property was already set to 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 be an alternative rendition of the other subparts of the multipart/alternative structure.

The ContentMediaType property of the new body part is set to application/octet-stream. This prevents the IMessage.HTMLBodyPart or IMessage.TextBodyPart property from interpreting the new body part as Hypertext Markup Language (HTML) or plain text if it is not one of these types. You should set the ContentMediaType property to the appropriate value when you obtain the body part from the AddBodyPart method.

Body parts that will contain attachments may also be added directly using the IMessage.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.IBodyPart

Set iBp = iMsg.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 = iMsg.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