0

如何在 C++ 中使用 MAPI 从 Outlook 配置文件中获取电子邮件地址?启动代码(工作正常):

    HRESULT hRes = S_OK;
LPMAPISESSION  lpSession = NULL;    
LPMDB          lpMDB    = NULL;
LPMAPITABLE    lptMsgStores = NULL; 
LPMAPITABLE spTable = NULL;

std::wstring wProfileName;
std::wstring wUsername;

wUsername    = L"user1@mymail.com";
wProfileName = L"TestProfile";

// Initiate MAPI.
hRes = MAPIInitialize(0);


// Logon to Extended MAPI session.
hRes = MAPILogonEx(NULL, 
    (LPTSTR)wProfileName.c_str(),
    NULL,
    MAPI_EXTENDED | MAPI_EXPLICIT_PROFILE | MAPI_NEW_SESSION | MAPI_UNICODE | MAPI_LOGON_UI, &lpSession);

    if(FAILED(hRes))
    {
        MessageBox(NULL,L"logon error", L"",MB_OK);
    }

LPOLKACCOUNTMANAGER lpAcctMgr = NULL;
hRes = CoCreateInstance(CLSID_OlkAccountManager, 
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IOlkAccountManager, 
    (LPVOID*)&lpAcctMgr);

if(SUCCEEDED(hRes) && lpAcctMgr)
{
    CAccountHelper *pMyAcctHelper = new CAccountHelper((LPWSTR)wProfileName.c_str(), lpSession);
    if(pMyAcctHelper)
    {
        LPOLKACCOUNTHELPER lpAcctHelper = NULL;
        hRes = pMyAcctHelper->QueryInterface(IID_IOlkAccountHelper, (LPVOID*)&lpAcctHelper);
        if(SUCCEEDED(hRes) && lpAcctHelper)
        {
            LPOLKENUM lpAcctEnum = NULL;
            hRes = lpAcctMgr->EnumerateAccounts(&CLSID_OlkMail,
                NULL,
                OLK_ACCOUNT_NO_FLAGS,
                &lpAcctEnum); //THIS FAILS HERE, hRes != S_OK!
            _com_error err(hRes);
            LPCTSTR errMsg = err.ErrorMessage();
            wprintf(L"%s\n", errMsg);
            if(SUCCEEDED(hRes) && lpAcctEnum)
            {
                DWORD cAccounts = 0 ;
                hRes = lpAcctEnum->GetCount(&cAccounts);
                if(SUCCEEDED(hRes))
                {
                    hRes = lpAcctEnum->Reset();
                    if(SUCCEEDED(hRes))
                    {
                        DWORD i = 0;
                        for(i  = 0 ; i< cAccounts; i++)
                        {
                            LPUNKNOWN lpUnk = NULL;
                            hRes = lpAcctEnum->GetNext(&lpUnk);
                            if(SUCCEEDED(hRes) &&lpUnk)
                            {
                                LPOLKACCOUNT lpAccount = NULL;
                                hRes  = lpUnk->QueryInterface(IID_IOlkAccount, (LPVOID*)&lpAccount);
                                if(SUCCEEDED(hRes) && lpAccount)
                                {
                                    ACCT_VARIANT pProp = {0};
                                    HRESULT hRes = S_OK;
                                    hRes = lpAccount->GetProp(PROP_ACCT_NAME, &pProp);
                                    if(SUCCEEDED(hRes) && pProp.Val.pwsz)
                                    {
                                        wprintf(L"Found email:%s\n", pProp.Val.pwsz);
                                        lpAccount->FreeMemory((LPBYTE)pProp.Val.pwsz);
                                    }
                                }
                                if(lpAccount)
                                    lpAccount->Release();
                                lpAccount = NULL;
                            }
                            if(lpUnk)
                                lpUnk->Release();
                            lpUnk = NULL;
                        }
                        ///////////
                    }
                }
            }
            if(lpAcctEnum)
                lpAcctEnum->Release();
        }

    }
    if(pMyAcctHelper)
        pMyAcctHelper->Release();
}
if(lpAcctMgr)
    lpAcctMgr->Release();


// Release the session.
lpSession->Logoff(0,MAPI_LOGOFF_UI,0);
lpSession->Release();

MAPIUninitialize();
_getch();       
4

1 回答 1

1

您不会将电子邮件地址添加到配置文件中,而是添加可能(POP3/IMAP4/SMTP)或可能不(PST)公开或需要 SMTP 地址的服务。对于 POP3/IMAP4/SMTP 地址,请使用IOlkAccountManager API。您可以在OutlookSpy中使用它(单击 IOlkAccountManager 按钮)。

如果您使用的语言不是 C++(或 Delphi)而不能使用 IOlkAccountManager 接口,您可能需要查看Redemption及其RDOSession.Accounts集合

于 2013-11-13T16:04:28.853 回答