Platform SDK: Exchange 2000 Server

IBodyPart Interface

[This is preliminary documentation and subject to change.]

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

IID
CD000021-8B95-11D1-82DB-00C04FB1625D
Extends
IDispatch

Member Summary

Properties

Name Type Description
BodyParts

(Read-Only)

[Visual Basic] IBodyParts

[C++] IBodyParts*

The BodyParts collection for this object.
Charset

[Visual Basic] String

[C++] BSTR

The character set for the text type body parts.
ContentClass

[Visual Basic] String

[C++] BSTR

The content class for the body part.
ContentClassName

[Visual Basic] String

[C++] BSTR

Deprecated. Not used.
ContentMediaType

[Visual Basic] String

[C++] BSTR

The content type and subtype of the body part.
ContentTransferEncoding

[Visual Basic] String

[C++] BSTR

The encoding mechanism used when encoding the body part content.
DataSource

(Read-Only)

[Visual Basic] IDataSource

[C++] IdataSource*

The IDataSource interface on the object.
Fields

(Read-Only)

[Visual Basic] ADODB.Fields

[C++] Fields*

The Fields collection for the object.
FileName

(Read-Only)

[Visual Basic] String

[C++] BSTR

The filename parameter attribute commonly used with the Content-Disposition header field.
Parent

(Read-Only)

[Visual Basic] IBodyPart

[C++] IBodyPart*

The parent body part object for the object.

Methods

Name Description
AddBodyPart Adds a BodyPart object to the BodyParts collection and returns a reference to the newly added object.
GetDecodedContentStream Returns an ADO Stream object (exposing the _Stream interface) containing the body part content in decoded format.
GetEncodedContentStream Returns an ADO Stream object (exposing the _Stream interface) containing the body part content in encoded format.
GetFieldParameter Returns the specified parameter value for the specified MIME header field.
GetInterface Returns the specified dual interface on the object.
GetStream Returns an ADO Stream object (exposing the _Stream interface) containing the body part contents.
SaveToFile Saves the decoded body part contents to a disk file.

Remarks

A message body that contains more that 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 IMessage.MIMEFormatted property.

You can also use the GetStream method to return the entire body part and all sub-parts in serialized, encoded format. For MIME-formatted messages, this stream will contain 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 ones 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.

Example

The following example demonstrates manually constructing the MIME structure for a message that contains two alternate 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.

  1. Create a Message object.
  2. 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.
  3. 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."
  4. Create a third BodyPart object at the first level and add an image (.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@microsoft.com"
   .To      = "PersonA@microsoft.com, PersonB@microsoft.com"
   .Subject = "A html and text message with attachment."
End With

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


''''''''''''''''''''''''''''''''
' Level 2: multipart/alternative
''''''''''''''''''''''''''''''''
Set iBp2 = iBp.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 plain text version of the message</p></body></html>"
   Stm.Flush
End With


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

Debug.Print iMsg.GetStream.ReadText
[C++,IDL]
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace  
#include <iostream.h>

void main()
{
  CoInitialize(NULL);
  {
    IMessagePtr iMsg(__uuidof(Message));
    IBodyPartPtr iBp;
    IBodyPartPtr iBp2;
    IBodyPartPtr iBp3;
    _StreamPtr   Stm;

    iMsg->From    = "Sender@microsoft.com";
    iMsg->To      = "PersonA@microsoft.com, PersonB@microsoft.com";
    iMsg->Subject = "A html and text message with attachment.";

    /*''''''''''''''''''''''''''''''''
    ' Level 1: multipart/mixed (root)
    ''''''''''''''''''''''''''''''''*/
    iBp = iMsg;        
    iBp->ContentMediaType = "multipart/mixed";


    /*''''''''''''''''''''''''''''''
    ' Level 2: multipart/alternative
    ''''''''''''''''''''''''''''''''*/
    iBp2 = iBp->AddBodyPart(-1);
    iBp2->ContentMediaType = "multipart/alternative";


    /*'''''''''''''''''''''''''
    ' Level 3: text/plain
    ''''''''''''''''''''''''''*/
    iBp3 = iBp2->AddBodyPart(-1);
    iBp3->ContentMediaType        = "text/plain";
    iBp3->ContentTransferEncoding = "7bit";
    Set Stm = iBp3->GetDecodedContentStream();
    Stm->WriteText(
                   "Here is the plain text version of the message",
                   adWriteChar
                  );
    Stm->Flush();


    /*'''''''''''''''''''''''''
    ' Level 3: text/html
    '''''''''''''''''''''''''*/
    iBp3 = iBp2->AddBodyPart(-1);
    iBp3->ContentMediaType        = "text/html";
    iBp3->ContentTransferEncoding = "quoted-printable";
    Stm = iBp3->GetDecodedContentStream();
    Stm->WriteText(
                   "<html><body><p>Here is the plain text version of the message</p></body></html>",
                   adWriteChar
                  );
    Stm->Flush();


    /*''''''''''''''''''''''''''''
    ' Level 2: image/gif
    '''''''''''''''''''''''''''''*/
    iBp2 = iBp->AddBodyPart(-1);
    iBp2->ContentMediaType = "image/gif";
    iBp2->ContentTransferEncoding = "base64";
    Stm = iBp2->GetDecodedContentStream();
    Stm->LoadFromFile("c:\somewhere\myimage.gif");
    Stm->Flush();

    // print message stream
    cout << iMsg->GetStream()->ReadText(-1) << endl;
  }
  CoUninitialize();
}
[VBScript]
Dim iMsg
Dim iBp1
Dim iBp2
Dim iBp3
Dim Stm

Set iMsg = CreateObject("CDO.Message")

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

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


''''''''''''''''''''''''''''''''
' Level 2: multipart/alternative
''''''''''''''''''''''''''''''''
Set iBp2 = iBp.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 plain text version of the message</p></body></html>"
   Stm.Flush
End With


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

Wscript.Echo iMsg.GetStream.ReadText