Extracting Embedded Messages

Topic Last Modified: 2006-06-12

Use the IDataSource.OpenObject method to extract messages that are contained within other objects, such as Message or BodyPart objects, and move them into another Message object. This method is most useful when you want to extract an embedded message from another Message object's BodyPart hierarchy. The process is analogous to opening files in Microsoft® Word: the content in the opened file is copied from the disk into the open application. Embedded messages are contained in body parts with Content-Media-Type set to "message."

To open an embedded message

  1. Find the BodyPart (IBodyPart Interface) object that contains the embedded message. Call the Message object with the embedded message "message A." The urn:schemas:mailheader:content-type (Content-Type) 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 instance or use an existing instance of the Message Component Object Model (COM) class. Use this object to extract and hold the embedded message. Call this object Message B.
  3. Using the IDataSource interface on Message B, invoke its 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 implementation 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) in Message A are now present in Message B. In effect, Message B is a duplicate of Message A.
  5. If you want to alter the contents of the embedded message with changes you make in Message B, simply call the IDataSource.Save method. The changes are copied back to the source object from which you opened the message.

Example

Visual Basic

' Reference to Microsoft ActiveX Data Objects 2.5 Library
' Reference to Microsoft CDO for Exchange 2000 Server Library

' It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
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 "c:\program files\common files\microsoft shared\cdo\cdoex.dll" no_namespace

// It is recommended that all input parameters be validated when they are
// first obtained from the user or user interface.
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

' It is recommended that all input parameters be validated when they are
' first obtained from the user or user interface.
Function ExtractMessage(iBp As IBodyPart) As Message
    Dim iMsg
    Set iMsg = CreateObject("CDO.Message")
    Dim iDsrc
    Set iDsrc = iMsg.DataSource
    iDsrc.OpenObject iBp, "IBodyPart"
    Set ExtractMessage = iMsg
End Function