使用Mapi发送邮件并回复多封电子邮件

时间:2011-06-27 08:35:52

标签: c++ mapi

我正在使用C ++中的MAPI库来发送电子邮件。现在我需要我发送的电子邮件回复设置为多封电子邮件,我只能将其发送到一封电子邮件。

我一直在阅读能够做到这一点我需要使用对象FLATENTRYLIST(link)和FLATENTRY(link)。 我怀疑的是如何在FLATENTRYLIST中存储多个FLATENTRY对象。我在C ++方面的经验并不是很高,所以如果有人能帮助我,我会感激不尽。

提前致谢
圣保罗

2 个答案:

答案 0 :(得分:1)

FLATENTRYLIST有cEntries成员,用于确定列表中的条目数。 您只需要将条目存储在abEntries数组中。

答案 1 :(得分:1)

以下是我开发的解决方案 - http://www.codeproject.com/KB/IP/CMapiEx.aspx?msg=3959770#xx3959770xx

感谢德米特里的帮助。

  
    

当我将EntryID复制到FlatEntry I时,复制错误的字节数。这是最终的代码,就像魅力一样。

  
BOOL CMAPIMessage::SetReplyTo(LPADRLIST lpAddrList)
{
    HRESULT hRes = S_OK;
    SPropValue pspvReply[1];
    LPFLATENTRYLIST lpEntryList = NULL;
    BOOL bResult = false;
    CString m_strReplyToNames;

    int cb = 0; 
    int displayNameTagID = -1;
    int emailTagID = -1;
    int entryIDTagID = -1;
    int cbAllBytes = 0;

    //Get all the EntryID's bytes to initalize the FLATENTRYLIST
    for(unsigned j=0; j<lpAddrList->cEntries; j++)
    {
        for (unsigned i = 0; i < lpAddrList->aEntries[j].cValues; i++)
        {
            if (lpAddrList->aEntries[j].rgPropVals[i].ulPropTag == PR_ENTRYID)
            {
                entryIDTagID = i;
            }
        }

        if (entryIDTagID >= 0)
        {
            int feBytes = CbNewFLATENTRY(lpAddrList->aEntries[j].rgPropVals[entryIDTagID].Value.bin.cb);

            cbAllBytes += feBytes;
            cbAllBytes += ((feBytes + 4 & ~3) - feBytes);
        }   
    }

    //Allocate a new FLATENTRYLIST with all flatentries bytes
    cb = CbNewFLATENTRYLIST(cbAllBytes);
    hRes = MAPIAllocateBuffer(cb, (LPVOID *)&lpEntryList);
    if (FAILED(hRes))
    {
        bResult = false;    
        goto Cleanup;
    }

    ZeroMemory((VOID *)lpEntryList, cb);

    // Copy the bits of the FLATENTRY into the FLATENTRYLIST.
    lpEntryList->cEntries  = lpAddrList->cEntries;

    int countBytesAdded = 0;

    for(unsigned j=0; j<lpAddrList->cEntries; j++)
    {
        for (unsigned i = 0; i < lpAddrList->aEntries[j].cValues; i++)
        {
            if (lpAddrList->aEntries[j].rgPropVals[i].ulPropTag == PR_TRANSMITABLE_DISPLAY_NAME)
            {
                displayNameTagID = i;
            }
            else if (lpAddrList->aEntries[j].rgPropVals[i].ulPropTag == PR_EMAIL_ADDRESS)
            {
                emailTagID = i;
            }
            else if (lpAddrList->aEntries[j].rgPropVals[i].ulPropTag == PR_ENTRYID)
            {
                entryIDTagID = i;
            }
        }

        if ((emailTagID>=0) && (entryIDTagID>=0))
        {
            CString m_strReplyToName;
            CString m_strReplyToEmail;

            m_strReplyToEmail=lpAddrList->aEntries[j].rgPropVals[emailTagID].Value.lpszA;

            if(displayNameTagID>=0) 
                m_strReplyToName=lpAddrList->aEntries[j].rgPropVals[displayNameTagID].Value.lpszA;
            else
                m_strReplyToName = m_strReplyToEmail;

            m_strReplyToNames += (CString)m_strReplyToName + ";";

            // Allocate a new FLATENTRY structure for the PR_REPLY_RECIPIENT_ENTRIES property
            LPFLATENTRY lpReplyEntry = NULL;

            cb = CbNewFLATENTRY(lpAddrList->aEntries[j].rgPropVals[entryIDTagID].Value.bin.cb);
            hRes = MAPIAllocateBuffer(cb, (LPVOID *)&lpReplyEntry);
            if (FAILED(hRes))
            {
                bResult = false;    
                goto Cleanup;
            }

            ZeroMemory((VOID *)lpReplyEntry, cb);

            // Copy the bits of the entry id into the FLATENTRY structure
            CopyMemory(lpReplyEntry->abEntry, 
                lpAddrList->aEntries[j].rgPropVals[entryIDTagID].Value.bin.lpb, 
                                lpAddrList->aEntries[j].rgPropVals[entryIDTagID].Value.bin.cb);

            lpReplyEntry->cb = lpAddrList->aEntries[j].rgPropVals[entryIDTagID].Value.bin.cb;

            int missingBytes  = 0;
            int feBytes = CbFLATENTRY(lpReplyEntry);

            missingBytes = ((feBytes + 4 & ~3) - feBytes);

            //Copy each FLATENTRY to the abEntries, next to the previous added bytes
            CopyMemory(lpEntryList->abEntries + countBytesAdded, lpReplyEntry, feBytes);

            countBytesAdded += feBytes + missingBytes; 

            //Clean Memory
            if (lpReplyEntry) MAPIFreeBuffer(lpReplyEntry);
        }

        displayNameTagID = -1;
        emailTagID = -1;
        entryIDTagID = -1;
    }

    lpEntryList->cbEntries = countBytesAdded;

    pspvReply[0].ulPropTag = PR_REPLY_RECIPIENT_ENTRIES;

    // Allocate memory in the lpb to hold the FLATENTRYLIST
    hRes = MAPIAllocateBuffer(CbFLATENTRYLIST(lpEntryList), (LPVOID *)&pspvReply[0].Value.bin.lpb);
    if (FAILED(hRes))
    {
        bResult = false;    
        goto Cleanup;
    }

    // Copy the memory into the SPropValue
    CopyMemory(pspvReply[0].Value.bin.lpb,lpEntryList,CbFLATENTRYLIST(lpEntryList));
    pspvReply[0].Value.bin.cb = CbFLATENTRYLIST(lpEntryList);

    //Set the PR_REPLY_RECIPIENT_NAMES property with all names
    SetPropertyString(PR_REPLY_RECIPIENT_NAMES, m_strReplyToNames);

    // Set the property that contains the FLATENTRYLIST with the reply-to adresses to the message properties
    hRes = Message()->SetProps(1,pspvReply,NULL);
    if (FAILED(hRes))
    {
        bResult = false;    
        goto Cleanup;
    }

    bResult = true; 

Cleanup:
    if (lpEntryList) MAPIFreeBuffer(lpEntryList);

    return bResult;
}