Microsoft KB Archive/300531

From BetaArchive Wiki
Knowledge Base


Article ID: 300531

Article Last Modified on 8/25/2005



APPLIES TO

  • Microsoft Messaging Application Programming Interface



This article was previously published under Q300531


SUMMARY

This step-by-step article describes how to use MAPI to set up alternate replays to a recipient.

Sending a message that has an alternate "Reply To" recipient through MAPI requires that you set two MAPI properties on a preconfigured message. These properties are PR_REPLY_RECIPIENT_NAMES and PR_REPLY_RECIPIENT_ENTRIES.

back to the top

Sample Code

NOTE: This code assumes that you have already established a valid MAPI session and have created and configured a MAPI message.

/**************************************************************************

  SetReplyTo

  Parameters:
        lpSession   MAPI Session that we are currently working with
        lpMessage   MAPI Message to set a "reply to" recipient on
        pszReplyTo  String holding the recipient to be set as "reply to"
                    Example: "John Doe [SMTP:johndoe@nowhere.com]"

  Return value:
        hRes        Result of the function

  NOTE: You must #include mapidefs.h, as well as mapiutil.h and mapix.h
**************************************************************************/ 


HRESULT SetReplyTo(LPMAPISESSION lpSession, LPMESSAGE lpMessage, LPSTR pszReplyTo)
{
    HRESULT hRes = S_OK;
    LPADRLIST lpAddrList = NULL;
    LPADRBOOK lpAddrBook = NULL;
    SPropValue pspvReply[2];
    LPFLATENTRYLIST lpReplyEntryList = NULL;
    LPFLATENTRY lpReplyEntry = NULL;
    int cValues = 2;
    ULONG i = 0;

    // Allocate an address list structure.
    int cb = CbNewADRLIST(1);

    hRes = MAPIAllocateBuffer(cb, (LPVOID *)&lpAddrList);   
    if (FAILED(hRes)) goto Cleanup;

    ZeroMemory(lpAddrList, cb);

    hRes = MAPIAllocateBuffer(
        cValues * sizeof(SPropValue), 
        (LPVOID FAR *)&lpAddrList->aEntries[0].rgPropVals);
    if (FAILED(hRes)) goto Cleanup;

    // Open the address book to resolve Reply To recipient.
    hRes = lpSession->OpenAddressBook(0, NULL, 0, &lpAddrBook);
    if (FAILED(hRes)) goto Cleanup;

    // Set up address list structure.
    lpAddrList->cEntries = 1;
    lpAddrList->aEntries[0].rgPropVals[0].ulPropTag = PR_DISPLAY_NAME;   
    lpAddrList->aEntries[0].rgPropVals[0].Value.lpszA = pszReplyTo;

    lpAddrList->aEntries[0].rgPropVals[1].ulPropTag = PR_RECIPIENT_TYPE;
    lpAddrList->aEntries[0].rgPropVals[1].Value.l = MAPI_TO;
    lpAddrList->aEntries[0].cValues = cValues;

    // Resolve name.
    hRes = lpAddrBook->ResolveName(0, 0, NULL, lpAddrList);
    if (FAILED(hRes)) goto Cleanup;

    // Get the Entry ID.
    for (i = 0; i < lpAddrList->aEntries[0].cValues; i++)
    {
        if (lpAddrList->aEntries[0].rgPropVals[i].ulPropTag == PR_ENTRYID)
        {
            // Allocate a new FLATENTRY structure for 
            // the PR_REPLY_RECIPIENT_ENTRIES property.
            cb = CbNewFLATENTRY(
                   lpAddrList->aEntries[0].rgPropVals[i].Value.bin.cb);
            hRes = MAPIAllocateBuffer(cb, (LPVOID *)&lpReplyEntry);
            if (FAILED(hRes)) goto Cleanup;
            ZeroMemory((VOID *)lpReplyEntry, cb);

            // Copy the bits of the Entry ID into the FLATENTRY structure.
            CopyMemory(lpReplyEntry->abEntry, 
                       lpAddrList->aEntries[0].rgPropVals[i].Value.bin.lpb,
                       lpAddrList->aEntries[0].rgPropVals[i].Value.bin.cb);
            lpReplyEntry->cb = lpAddrList->
                                 aEntries[0].rgPropVals[i].Value.bin.cb;

            // Allocate a new FLATENTRYLIST structure.
            // This is what is stored in PR_REPLY_RECIPIENT_ENTRIES.
            cb = CbNewFLATENTRYLIST(cb);
            hRes = MAPIAllocateBuffer(cb, (LPVOID *)&lpReplyEntryList);
            if (FAILED(hRes)) goto Cleanup;
            ZeroMemory((VOID *)lpReplyEntryList, cb);

            // Copy the bits of the FLATENTRY into the FLATENTRYLIST.
            lpReplyEntryList->cEntries  = 1;
            lpReplyEntryList->cbEntries = CbFLATENTRY(lpReplyEntry);
            CopyMemory(lpReplyEntryList->abEntries, 
                       lpReplyEntry, CbFLATENTRY(lpReplyEntry));

            // Force exit from the loop.
            i = lpAddrList->aEntries[0].cValues;
        }
    }

    // Set PR_REPLY_RECIPIENT_NAMES.
    pspvReply[0].ulPropTag = PR_REPLY_RECIPIENT_NAMES;
    pspvReply[0].Value.lpszA = pszReplyTo;

    // Set PR_REPLY_RECIPIENT_ENTRIES.
    pspvReply[1].ulPropTag = PR_REPLY_RECIPIENT_ENTRIES;
    // Allocate memory in the lpb to hold the FLATENTRYLIST
    hRes = MAPIAllocateBuffer(CbFLATENTRYLIST(lpReplyEntryList), 
                              (LPVOID *)&pspvReply[1].Value.bin.lpb);
    // Copy the memory into the SPropValue.
    CopyMemory(pspvReply[1].Value.bin.lpb,
               lpReplyEntryList,
               CbFLATENTRYLIST(lpReplyEntryList));
    pspvReply[1].Value.bin.cb = CbFLATENTRYLIST(lpReplyEntryList);

    // Set the properties.
    hRes = lpMessage->SetProps(2,
                               pspvReply,
                               NULL);

Cleanup:
    if (lpReplyEntry) MAPIFreeBuffer(lpReplyEntry);
    if (lpReplyEntryList) MAPIFreeBuffer(lpReplyEntryList);
    if (lpAddrList) FreePadrlist(lpAddrList);
    if (lpAddrBook) lpAddrBook->Release();
    return hRes;
}
                

back to the top

REFERENCES

200174 HOWTO: Create a Message With an Attachment and Send It Using MAPI/VC++


181408 HOWTO: Use CDO (1.x) to Set Up Reply to Alternate Recipient


back to the top

Keywords: kbhowto kbhowtomaster kbmsg KB300531