Logging On to a Mailbox Using the IExchangeManageStore Interface

The following procedure uses the CreateStoreEntryID method of the IExchangeManageStore interface to log on to a mailbox.

    To log on to a mailbox using the IExchangeManageStore interface
  1. Create a profile that lists an information store to which your service will log on. The service’s Windows NT security ID should be listed as the owner of the store. This store is usually not used for any purpose other than logging on to MAPI.
  2. Initialize MAPI using MAPIInitialize.
  3. Log on to MAPI using MAPILogonEx and specify the previously created profile.
  4. Get the entry identifier of the default information store, which in this case is the Microsoft Exchange Server information store. Use the HrMAPIFindDefaultMsgStore function or make direct MAPI calls to perform this action.
  5. Open the mailbox listed in the profile using the IMAPISession::OpenMsgStore function. Microsoft Exchange Server examines the Windows NT security identifier under which your application is running and compares this with the access control list (ACL) that has been set on the mailbox object in the Microsoft Exchange Server directory. If the application does not have the appropriate rights, the call to OpenMsgStore will fail.
  6. Obtain a pointer to IExchangeManageStore by calling the QueryInterface method of the message store object.
  7. Call IExchangeManageStore::CreateStoreEntryID, passing the distinguished names (DNs) of the server and the mailbox, as well as the privileges your service is to have for the mailbox. For example, you could specify an OR’d combination of bits such as OPENSTORE_USE_ADMIN_PRIVILEGE | OPENSTORE_TAKE_OWNERSHIP. The function formats an entry identifier and does not access the information store. This means calls to the CreateStoreEntryID function are relatively fast and require no server resources.
  8. Open the information store using the pointer to the entry identifier that is returned from CreateStoreEntryID. Do this by calling the IMAPISession::OpenMsgStore method. In this call, make sure to specify the flag MDB_TEMPORARY. Otherwise, the store you are opening may be added to your profile.

The following sample code shows how to implement the previously listed steps. The sample code is the implementation of the HrMailboxLogon function, located in \BKOFFICE\SAMPLES\EXCHANGE\LIBSRC\MBLOGON\MBLOGON.C.

Note The MBLOGON sample application includes error checking. For clarity, error checking has been removed from this code sample.

// -----------------------------------------------------------------
// HrMailboxLogon() without any error detection.
// -----------------------------------------------------------------

HRESULT HrMailboxLogon(
   IN  LPMAPISESSION   lplhSession,            // ptr to MAPI session handle
   IN  LPMDB           lpMDB,                  // ptr to message store
   IN  LPTSTR          lpszMsgStoreDN,         // ptr to server DN
   IN  LPTSTR          lpszMailboxDN,          // ptr to mailbox DN
   OUT LPEXCHANGEMANAGESTORE *lppXManageStore, // ptr to manage mailbox interface ptr
   OUT LPMDB           *lppMailboxMDB)         // ptr to mailbox message store ptr
{
    ULONG       cbeid   = 0;      // count of bytes in entry ID
    LPENTRYID   lpeid   = NULL;   // Entry ID of default store

    MAPICALL( lpMDB)->QueryInterface( lpMDB, (REFIID) &IID_IExchangeManageStore,
        (LPVOID*) lppXManageStore);

    MAPICALL( *lppXManageStore)->CreateStoreEntryID(
        *lppXManageStore, lpszMsgStoreDN, lpszMailboxDN,
        OPENSTORE_USE_ADMIN_PRIVILEGE | OPENSTORE_TAKE_OWNERSHIP,
        &cbeid, &lpeid);

    MAPICALL( lplhSession)->OpenMsgStore(
        lplhSession, 0, cbeid, lpeid, NULL, MDB_NO_DIALOG | MAPI_BEST_ACCESS, 
        lppMailboxMDB);

    MAPIFREEBUFFER( lpeid);
    
    return( NOERROR);
}