How to: Begin a MAPI Session

Before you can access a message store, you must initialize the MAPI subsystem and log onto a MAPI session. This gives you a referfence to an IMAPISession interface object, which you can use to access the message store table, message stores, message folders, and messages.

To begin the MAPI session

  1. Increment the MAPI subsystem reference count, and initialize global data for the MAPI DLL with the MAPIInitialize global MAPI function, as follows:

    hr = MAPIInitialize(NULL);
    
  2. Log onto a MAPI session with the MAPILogonEx global MAPI function, and get a reference to an ICEMAPISession interface object:

    hr = MAPILogonEx(0, NULL, NULL, 0, (LPMAPISESSION *)&pSession);
    

Example

The following code demonstrates how to begin a MAPI session.

HRESULT hr;
ICEMAPISession * pSession = NULL;

hr = MAPIInitialize(NULL);
if (hr != S_OK) {
    // MAPIInitialize failed.
    MessageBox(NULL, 
               _T("MAPIInitialize failed."),
               _T("Warning"), 
               MB_OK);
    exit(0);  // Replace with specific error handling.
}

hr = MAPILogonEx(0, NULL, NULL, 0, (LPMAPISESSION *)&pSession);
if (hr != S_OK) {
    // MAPILogonEx failed.
    MessageBox(NULL, 
               _T("MAPILogonEx failed."),
               _T("Warning"), 
               MB_OK);
    exit(0);  // Replace with specific error handling.
}

See Also

Messaging

Messaging Overview

How to: Connect to a Message Store

How to: Create a Message

How to: Send a Message

How to: End a MAPI Session

Messaging Sample Code

Last updated on Friday, April 22, 2005

© 2005 Microsoft Corporation. All rights reserved.

Send feedback on this topic to the authors.