HOWTO: Programmatically Embedding a Message in Another Message

ID: Q170226


The information in this article applies to:
  • Extended Messaging Application Programming Interface (MAPI), version 1.0


SUMMARY

This article shows the minimum code necessary to embed a message in a message.


MORE INFORMATION

The following function illustrates the code necessary to embed a source message object to an existing destination message object:


    HRESULT AttachMessage ( LPMESSAGE pMsgDst, // The message to attach to
                     LPMESSAGE pMsgSrc, // The message to attach
                     ULONG ulProps,     // Count of properties to set
                                        // on attachment
                     LPSPropValue pAttachProps // Properties to set
                                               // on attachment
                          )
    {
    // Initialize local variables.
    HRESULT   hRes        = S_OK;      // Status code of MAPI calls
    ULONG     ulAttachNum = 0L;        // Number of attachments
    LPATTACH  pAttach     = NULL;      // Attachment object
    LPMESSAGE pmsgAttach  = NULL;      // Message interface for
                                       // attachment object

    LPSPropProblemArray pProb = NULL;   // Problem array

    SizedSPropTagArray(7, excludeTags);  // Properties excluded by
                                         // Exchange client.

    //  Create an attachment in the Destintation message that was passed
    //  in to this function - pMsgDst.
    if ( FAILED ( hRes = pMsgDst -> CreateAttach ( NULL,
                                                   0L,
                                                   &ulAttachNum,
                                                   &pAttach ) ) )
         goto Quit;

    if ( FAILED ( hRes = pAttach -> SetProps ( ulProps,
                                                 pAttachProps,
                                                 &pProb ) ) )
         goto Quit;


    //  Need to open the data object property and get back an IMessage
    //  interface pointer so I can call CopyTo and pass it as the
    //  destination object.
    hRes = pAttach -> OpenProperty ( PR_ATTACH_DATA_OBJ,
                                     &IID_IMessage,
                                     0L,
                                     MAPI_CREATE|MAPI_MODIFY,
                                     (LPUNKNOWN *) &pmsgAttach );

    //  Must set cValues otherwise we will most likely get
    //  MAPI_E_INVALID_PARAMETER. Other tags are excluded to save bits
    //  and time.
    excludeTags.cValues = 7;
    excludeTags.aulPropTag[0] = PR_ACCESS;
    excludeTags.aulPropTag[1] = PR_BODY;
    excludeTags.aulPropTag[2] = PR_RTF_SYNC_BODY_COUNT;
    excludeTags.aulPropTag[3] = PR_RTF_SYNC_BODY_CRC;
    excludeTags.aulPropTag[4] = PR_RTF_SYNC_BODY_TAG;
    excludeTags.aulPropTag[5] = PR_RTF_SYNC_PREFIX_COUNT;
    excludeTags.aulPropTag[6] = PR_RTF_SYNC_TRAILING_COUNT;

    //  Call CopyTo from the attached message to copy all the properties
    //  from it to the destination object except the ones we asked to be
    //  excluded.
    if ( FAILED ( hRes = pMsgSrc -> CopyTo (0L,
                                            0L,
                                            (LPSPropTagArray)&excludeTag,
                                            0L,
                                            0L,
                                            &IID_IMessage,
                                            (LPVOID) pmsgAttach,
                                            0L,
                                            &pProb ) ) )
          goto Quit;

    //  Save the changes to the destination object, the attachment, and
    //  the outer message. Order is important.
    if(FAILED(hRes = pmsgAttach->SaveChanges ( KEEP_OPEN_READWRITE ) ) )
    {
          MessageBox ( (ULONG *)m_hWnd,
                       "Cannot save changes to message attachment",
                       "Error", MB_OK | MB_ICONSTOP );
          goto Quit;
    }

    if ( FAILED (hRes = pAttach->SaveChanges ( KEEP_OPEN_READWRITE ) ) )
    {
          MessageBox ( (ULONG *)m_hWnd,
                       "Cannot save changes to attachment.",
                       "Error", MB_OK | MB_ICONSTOP );
          goto Quit;
    }

    if ( FAILED ( hRes = pMsgDst->SaveChanges ( KEEP_OPEN_READWRITE ) ) )
    {
          MessageBox ( (ULONG *)m_hWnd,
                       "Cannot save changes to message.",
                       "Error", MB_OK | MB_ICONSTOP );
          goto Quit;
    }


    Quit:

      pmsgAttach -> Release ( );
      pAttach -> Release ( );
      return hRes;
    } 

Additional query words:

Keywords : kbMsg kbMAPI100
Version : WINDOWS:1.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 9, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.