Platform SDK: CDO for Windows 2000

Extracting Embedded Messages

You can use the IDataSource.OpenObject method to extract messages contained within the body parts of other Message objects. The process is analogous to opening files in Microsoft Word: the content in the opened file is copied from the disk into the running application. Embedded messages in other messages are contained in body parts with Content-Type message/*. When you encounter such a body part, you can extract it with the following steps:

  1. Find the BodyPart (IBodyPart interface) object that contains the embedded message within the first message, called Message A. The urn:schemas:mailheader:content-type (Content-Type) field for the body part should contain the "message" media type. The full Content-Type is normally message/rfc822 for a fully embedded message (as opposed to partial or external-body subtypes).
  2. Create a new Message object that we will call Message B. You will use this object to extract the embedded message.
  3. Using the IDataSource interface on Message B, call the IDataSource.OpenObject method, passing the IBodyPart interface retrieved in Step 1 as the first argument, and the string "IBodyPart" as the second argument. The string "IBodyPart" notifies the object that the passed interface reference is of type IBodyPart and therefore, to expect a body part content stream to exist in the object.
  4. If the call is successful, the entire contents of the embedded message (including all attachments, and so on) encapsulated in Message A are now present as Message B.
  5. If you want to alter the contents of the embedded message with changes you make in Message B, you call the IDataSource.Save method and the changes are copied back to source object from which you opened the message.
[Visual Basic]
' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Windows 2000 Library
Function ExtractMessage(iBp As IBodyPart) As Message
    Dim iMsg As New CDO.Message
    Dim iDsrc As IDataSource
    Set iDsrc = iMsg
    iDsrc.OpenObject iBp, "IBodyPart"
    Set ExtractMessage = iMsg
End Function
[C++,IDL]
#import "c:\program files\common files\system\ado\msado15.dll" no_namespace
#import <cdosys.dll> no_namespace
// ...
IMessagePtr ExtractMessage(IBodyPartPtr iBp)
{
      IMessagePtr iMsg(__uuidof(Message));
      IDataSourcePtr iDsrc;
      iDsrc = iMsg;

      try 
      {      
            iDsrc->OpenObject(iBp,_bstr_t("IBodyPart"));
      }
      catch(_com_error error)
      {
            throw error;
      }
      return iMsg;
}
[VBScript]
Function ExtractMessage(iBp)
    Dim iMsg
    Set iMsg = CreateObject("CDO.Message")
    Dim iDsrc
    Set iDsrc = iMsg.DataSource
    iDsrc.OpenObject iBp, "IBodyPart"
    Set ExtractMessage = iMsg
End Function

See Also

IDataSource Interface

IDataSource.OpenObject Method

urn:schemas:mailheader:content-type Field