Platform SDK: Exchange 2000 Server

Extracting Embedded Messages

[This is preliminary documentation and subject to change.]

Use the IDataSource.OpenObject method to extract messages contained within other objects, such as Message or BodyPart objects, and move them into another Message object. This method is most useful when you wish to extract an embedded message from within some other 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 running application. Embedded messages in other messages are contained in body parts with Content-Media-Type set to "message." When you encounter such a message in a body part, you can open it in the following way:

  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 COM class. You will 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, etc) in Message A are now present in Message B. In effect, Message B is now that message.
  5. If you wish to alter the contents of the embedded message with changes you make in Message B, you simply 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 Exchange 2000 Server 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 "c:\exchsrvr\cdoex.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 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

See Also

urn:schemas:mailheader:content-type Field