Extracting the e-mail addresses and the associated NT accounts from the global address list is easier to program and difficult to execute. Organizations have huge list of addresses to the tune of 50 to 100 thousands. But if the numbers are small, this sample code MAPI C++ address book can be used to extract the address book information.
As in any other MAPI Client program, this also requires initialization of MAPI libraries using MAPIInitialize() function. The next will be to logon to the mailbox using MAPILogonEx() which has the access to the Global Address List.
Then we use the pointer LPADRBOOK to the IAddrBook interface and get the global address list using HrFindExchangeGlobalAddressList. After this it is a classical case of getting the contents using GetContentsTable and HrQueryAllRows.
C++ MAPI Address Book – Sample:
The following sample code logs on to the profile and prints all the addresses in the organization.
Warning: This could take longer time, if the address list is in many thousands.
#include <edk.h>
LPMAPISESSION pSession = NULL;
int main()
{
HRESULT hr = S_OK;
//Initialize MAPI libraries
hr = MAPIInitialize (NULL);<
// logon to MAPI for extracting the address book. This can also be used to open the individual mail boxes.
hr = MAPILogonEx (NULL, "testprofile", NULL, MAPI_EXTENDED| MAPI_NEW_SESSION|
MAPI_LOGON_UI| MAPI_EXPLICIT_PROFILE,&pSession);
if (FAILED (hr))
{
return E_FAIL;
}
ULONG cbeid = 0L;
LPENTRYID lpeid = NULL;
LPSRowSet pRow;
ULONG ulObjType;
LPMAPITABLE lpContentsTable = NULL;
LPABCONT lpGAL = NULL;
LPADRBOOK m_pAddrBook = NULL;
SizedSPropTagArray ( 3, sptCols ) = { 3, PR_ENTRYID, PR_DISPLAY_NAME,PR_ACCOUNT };
hr = pSession->OpenAddressBook(NULL,NULL,AB_NO_DIALOG,&m_pAddrBook);
if ( FAILED ( hr = HrFindExchangeGlobalAddressList ( m_pAddrBook, &cbeid, &lpeid ) ) )
{ return hr; }
if(FAILED(hr = m_pAddrBook->OpenEntry((ULONG) cbeid, (LPENTRYID) lpeid, NULL, MAPI_BEST_ACCESS,&ulObjType, (LPUNKNOWN *)&lpGAL)))
{ return hr; }
if ( ulObjType != MAPI_ABCONT )
{ return -2323; }
//Free the entry id buffer
::MAPIFreeBuffer(lpeid);
if(FAILED(hr = lpGAL->GetContentsTable(0L, &lpContentsTable)))
{ return hr; }
if ( FAILED ( hr = HrQueryAllRows (lpContentsTable,(SPropTagArray*) &sptCols, NULL, NULL, 0,&pRow)))
{ return hr; }
char strName[200];
for(int x=0; x < pRow->cRows ;x++)
{
printf("%sn",pRow->aRow[x].lpProps[1].Value.lpszA);
}
if ( NULL != lpGAL)
{
lpGAL -> Release ( );
lpGAL = NULL;
}
if ( lpContentsTable )
{
lpContentsTable -> Release ( );
lpContentsTable = NULL;
}
m_pAddrBook->Release();
//Free all MAPI libraries
MAPIUninitialize();
}
The testprofile should be replaced with the suitable one from the local computer.
C++ MAPI Address Book – Libraries Required:
Add these libraries in the project settings–> Link –> Object/library modules.
mapi32.lib Edkguid.lib Edkutils.lib Edkmapi.lib Addrlkup.lib Edkdebug.lib Version.lib Mblogon.lib.
Also in the Project Settings –> Link –> Category –> Input –>Ignore Libraries , add libc.lib and libcd.lib.
When the program is compiled, if the ” LINK : fatal error LNK1104: cannot open the file “mfc40.lib” ” is seen, then this problem has to be resolved by Recompiling the MAPI Exchange Development Kit.