Logging On to a Mailbox as a Privileged Service

The HrMailboxLogon and HrMailboxLogoff functions are designed to simplify the task of performing basic mailbox management. The sample code in the following section shows how to implement the two functions listed.

    To log on to a mailbox as a privileged service
  1. Open the default information store or another information store. For more information, see Opening an Information Store. This action creates the information store object and returns a LPMDB pointer to it.
  2. Call the HrMailboxLogon function. The following sample code shows how to log on to a mailbox using the HrMailboxLogon function. (For more granular control, use the IExchangeManageStore interface. See Using the IExchangeManageStore Interface.)

The full source code for this sample application is located in BKOFFICE\SAMPLES\EXCHANGE\MBLSAMPL\MBLSAMPL.C. For clarity, error handling is not included in this code sample.

// --------------------------------------------------------------
// Opening a mailbox using privileged access - without error detection.
// --------------------------------------------------------------

AnyFunction()
{
    LPTSTR          lpszProfileName = "Admin Profile";
    LPTSTR          lpszPassword    = NULL;
    LPTSTR          lpszServer      = "/O=Organization/OU=Site/CN=Servers/CN=ServerName/CN=NULL";
    LPTSTR          lpszMailbox     = "/O=Organization/OU=Site/CN=Recipients/CN=MailboxName";
    LPMAPISESSION   lplhSession     = NULL;
    LPMDB           lpMDB           = NULL;     // Ptr to default msg store interface.
    LPMDB           lpMailboxMDB    = NULL;     // Ptr to any another's msg store interface.
    ULONG           cbeid           = 0;      
    LPENTRYID       lpeid           = NULL;   

    MAPIInitialize( NULL);

    MAPILogonEx( 0, lpszProfileName, lpszPassword, 
                 MAPI_NEW_SESSION | MAPI_LOGON_UI | MAPI_EXTENDED, &lplhSession);

    HrMAPIFindDefaultMsgStore( lplhSession, &cbeid, &lpeid);

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

    HrMailboxLogon( lplhSession, lpMDB, lpszMsgStoreDN, lpszMailboxDN, &lpMailboxMDB);

    // Now lpMailboxMDB contains the MAPI interface needed to access the mail box requested.
    ...
}