Registrations are now open. Join us today!
There is still a lot of work to do on the wiki yet! More information about editing can be found here.
Already have an account?

Microsoft KB Archive/170226

From BetaArchive Wiki
Knowledge Base


How To Programmatically Embedding a Message in Another Message

Article ID: 170226

Article Last Modified on 8/18/2005



APPLIES TO

  • Microsoft Messaging Application Programming Interface



This article was previously published under Q170226

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;
    }
                

Keywords: kbhowto kbmsg KB170226