#include <stdio.h>
#include <edk.h>
HRESULT OpenMailbox(LPMAPISESSION lpMAPISession,
LPSTR pszExchangeServerName);
void main()
{
HRESULT hr = S_OK;
hr = MAPIInitialize(NULL);
if(FAILED(hr))
{
printf("Failed to initialize MAPI\n");
}
char pszExchangeServerName[500];
LPMAPISESSION lpSess = NULL;
LPMDB lpMDB = NULL;
// Get the Exchange Server name from the user.
//
printf("n\nPlease enter the name of your Exchange System ? ");
gets(pszExchangeServerName);
printf("\n\n");
// TO DO: Need to logon using a profile for the service account or
// an Exchange Admin.
hr = MAPILogonEx(0, "", NULL,
MAPI_LOGON_UI | MAPI_NEW_SESSION | MAPI_EXPLICIT_PROFILE ,
&lpSess);
if (FAILED(hr))
{
MessageBox(NULL,"MAPI Logon failed",NULL,MB_OK);
}
if(SUCCEEDED(hr)&& lpSess)
{
printf("Created MAPI session\n");
hr = OpenMailbox(lpSess, pszExchangeServerName);
if(FAILED(hr))
printf("Failed to Run\n");
else
printf("Opened users mailboxes\n");
}
char ch;
printf("\nHit a key to exit");
ch = getchar();
if(lpSess)
lpSess->Release();
}
HRESULT OpenMailbox(LPMAPISESSION lpMAPISession, LPSTR pszServerName)
{
HRESULT hr = S_OK;
LPMAPITABLE lpMailBoxTable = NULL;
LPSRowSet lpRows = NULL;
LPENTRYID lpMsgStoreID = NULL;
ULONG cbMsgStoreID = 0;
LPMDB lpMDB = NULL;
LPMDB lpUserMDB = NULL;
LPMAPIFOLDER lpFolder = NULL;
LPEXCHANGEMANAGESTORE lpIManageStore = NULL;
char pszServerDN[500];
if (FAILED(hr = HrOpenExchangePrivateStore(lpMAPISession, &lpMDB)))
{
MessageBox(0L,"Message Store Not Available","Error",MB_OK);
return MAPI_E_NOT_FOUND;
}
if (FAILED(hr = lpMDB->QueryInterface(IID_IExchangeManageStore,
(void **) &lpIManageStore)))
{
MessageBox(0L,"QueryInterace Failed","Error",MB_OK);
return MAPI_E_NOT_FOUND;
}
// TO DO: Create server DN. Replace "myorgname" and "mysitename"
// with appropriate organization and site name.
sprintf(pszServerDN,"/o=myorgname/ou=mysitename/cn=servers/cn=%s",
pszServerName);
if (FAILED(hr = lpIManageStore->GetMailboxTable(pszServerDN,
&lpMailBoxTable,0)))
{
MessageBox(0L,"Mailbox Table Not Available","Error",MB_OK);
return MAPI_E_NOT_FOUND;
}
// Get a list of Mailboxes taking up resources.
hr = HrQueryAllRows(lpMailBoxTable, NULL, NULL, NULL, 0, &lpRows);
if(SUCCEEDED(hr))
{
// TO DO: Create Information Store DN. Replace "myorgname"
// and "mysitename" with appropriate organization
// and site name.
sprintf(pszServerDN,
"/o=myorgname/ou=mysitename/cn=servers/cn=%s%s",
pszServerName,"/cn=Microsoft Private MDB");
if (lpRows->cRows > 0)
{
for (UINT i=0; i < lpRows->cRows; i++)
{
LPSPropValue lpspv = PpropFindProp(
lpRows->aRow[i].lpProps,
lpRows->aRow[i].cValues,
PR_EMAIL_ADDRESS );
if(FAILED(hr = HrMailboxLogon(lpMAPISession,
lpMDB,pszServerDN,lpspv->Value.lpszA,
&lpUserMDB)))
{
MessageBox(0L,"Mailbox Not Available","Error",MB_OK);
continue;
}
else
{
printf("Opened %s \n",lpspv->Value.lpszA);
}
// TO DO: ****** Place Mailbox Processing Here.
HrMailboxLogoff(&lpUseMDB);
}
}
}
if(lpRows)
{
FreeProws(lpRows);
}
if(lpMailBoxTable)
{
lpMailBoxTable->Release();
}
if (FAILED(hr))
return MAPI_E_NOT_FOUND;
else
return S_OK;
}