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

Send mail with Mapi with reply-to multiple emails

本文关键字:回复 电子邮件 Mapi 使用      更新时间:2023-10-16

我正在使用C++中的MAPI库发送电子邮件。现在我需要我发送的电子邮件有一个回复设置为多封电子邮件,我只需要对一封电子邮件进行回复。

我已经读到,为了能够做到这一点,我需要使用对象FLATENTRYLIST(链接)和FLATENTRY(链接)。我的疑问是如何在FLATENTRYLIST中存储多个FLATENTRY对象。我在C++方面的经验不是很丰富,所以如果有人能帮助我,我会感激的。

提前感谢
Paulo

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

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

感谢德米特里的帮助。

当我将EntryID复制到FlatEntry时,我复制了错误的字节数。这是最后一个像符咒一样工作的代码。

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