Export (0) Print
Expand All

Adding BodyPart Objects to the MIME Hierarchy

Exchange Server 2003

Topic Last Modified: 2004-06-08

The following task example demonstrates how to create a common MIME hierarchy for a message that contains two alternate representations of the message text: text/plain and text/html. The steps are as follows:

  1. Create the Message object and retrieve its IBodyPart interface.
  2. Use the IBodyPart.AddBodyPart method to create and add a child BodyPart object.
  3. Use the returned IBodyPart object reference to set fields for the object using its IBodyPart.Fields collection.
  4. If this body part is to directly contain content, add this content by retrieving a Stream object for the contents, using the IBodyPart.GetDecodedContentStream method. Write the data to the Stream object, and call the _Stream.Flush method to commit the stream to the BodyPart object.
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Dim iMsg As New CDO.Message
Dim iBp  As CDO.IBodyPart
Dim iBp1 As CDO.IBodyPart
Dim Flds As ADODB.Fields
Dim Stm  As ADODB.Stream

' Get IBodyPart on the Message object
Set iBp = iMsg

' TEXT BODYPART
' Add the body part for the text/plain part of message
Set iBp1 = iBp.AddBodyPart

' set the fields here
Set Flds = iBp1.Fields
Flds("urn:schemas:mailheader:content-type") = "text/plain; charset=""iso-8859-1"""
Flds.Update

' get the stream and add the message
Set Stm = iBp1.GetDecodedContentStream
Stm.WriteText "this is the message in text format"
Stm.Flush


' HTML BODYPART
' Do the HTML part here
Set iBp1 = iBp.AddBodyPart

' set the content-type field here
Set Flds = iBp1.Fields
Flds("urn:schemas:mailheader:content-type") = "text/html"
Flds.Update

' get the stream and add message HTML text to it
Set Stm = iBp1.GetDecodedContentStream
Stm.WriteText "<HTML><H1>this is some content for the body part object</H1></HTML>"
Stm.Flush


' Now set the Message object's Content-Type header
' to multipart/alternative
Set Flds = iBp.Fields
Flds("urn:schemas:mailheader:content-type") = "multipart/alternative"
Flds.Update

#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace
// ...
IMessagePtr iMsg(__uuidof(Message));
IBodyPartPtr iBp;
IBodyPartPtr iBp1;
FieldsPtr Flds;
_StreamPtr Stm;

// Get IBodyPart on the Message object
iBp = iMsg;

// TEXT BODYPART
// Add the body part for the text/plain part of message
iBp1 = iBp->AddBodyPart(-1);

' set the fields here
Flds = iBp1->Fields();
Flds->Item["urn:schemas:mailheader:content-type"]->Value
   = _variant_t("text/plain; charset=""iso-8859-1""");
Flds->Update();

// get the stream and add the message
Stm = iBp1->GetDecodedContentStream();
Stm->WriteText("this is the message in text format",adWriteLine);
Stm->Flush();

// HTML BODYPART
// Do the HTML part here
iBp1 = iBp->AddBodyPart(-1);

// set the content-type field here
Flds = iBp1->Fields;
Flds->Item["urn:schemas:mailheader:content-type"]->Value
   = _variant_t("text/html");
Flds->Update();

// get the stream and add message HTML text to it
Stm = iBp1->GetDecodedContentStream();
Stm->WriteText("<HTML><H1>this is some content for the body part object</H1></HTML>",adWriteLine);
Stm->Flush();


// Now set the Message object's Content-Type header
// to multipart/alternative
Flds = iBp->Fields;
Flds->Item["urn:schemas:mailheader:content-type"]->Value
     = _variant_t("multipart/alternative");
Flds->Update();
Const g_Debug = True
Dim iMsg
Dim iBp
Dim iBp1
Dim Flds
Dim Stm

Set iMsg = CreateObject("CDO.Message")
' Get IBodyPart on the Message object
Set iBp = iMsg.BodyPart

' TEXT BODYPART
' Add the body part for the text/plain part of message
Set iBp1 = iBp.AddBodyPart

' set the fields here
Set Flds = iBp1.Fields
Flds("urn:schemas:mailheader:content-type") = "text/plain; charset=""iso-8859-1"""
Flds.Update

' get the stream and add the message
Set Stm = iBp1.GetDecodedContentStream
Stm.WriteText "this is the message in text format"
Stm.Flush


' HTML BODYPART
' Do the HTML part here
Set iBp1 = iBp.AddBodyPart

' set the content-type field here
Set Flds = iBp1.Fields
Flds("urn:schemas:mailheader:content-type") = "text/html"
Flds.Update

' get the stream and add message HTML text to it
Set Stm = iBp1.GetDecodedContentStream
Stm.WriteText "<HTML><H1>this is some content for the body part object</H1></HTML>"
Stm.Flush


' Now set the Message object's Content-Type header
' to multipart/alternative
Set Flds = iBp.Fields
Flds("urn:schemas:mailheader:content-type") = "multipart/alternative"
Flds.Update

If g_Debug Then
 MsgBox iMsg.GetStream.ReadText
End If

Show:
© 2015 Microsoft