获取Outlook配置文件,MAPI的电子邮件地址 [英] Get email addresses of outlook profile, MAPI

查看:184
本文介绍了获取Outlook配置文件,MAPI的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How does one get the email addresses from an outlook profile, using MAPI, in C++ ? The code for start(works ok):

    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();       

推荐答案

您没有在配置文件中添加电子邮件地址,而是添加了可能(POP3/IMAP4/SMTP)或可能不会(PST)公开或需要一个SMTP地址.对于POP3/IMAP4/SMTP地址,请使用 IOlkAccountManager API.您可以在 OutlookSpy 中进行操作(单击IOlkAccountManager按钮).

You don't add an email address to a profile, you add a service that may (POP3/IMAP4/SMTP) or may not (PST) expose or need an SMTP address. For POP3/IMAP4/SMTP addresses, use IOlkAccountManager API. You can play with it in OutlookSpy (click IOlkAccountManager button).

如果您使用的是C ++(或Delphi)以外的语言,但无法使用IOlkAccountManager接口,则您可能需要研究兑换及其 RDOSession.Accounts 集合

If you are using language other than C++ (or Delphi) that cannot use the IOlkAccountManager interface, you might want to look into Redemption and its RDOSession.Accounts collection

这篇关于获取Outlook配置文件,MAPI的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆