Beginning a MAPI Session

Send Feedback

Before you can access a message store, you must initialize the MAPI subsystem and log onto a MAPI session. This gives you a reference 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);
    

Code Example

The following code example demonstrates how to begin a MAPI session.

Note   To make the following code example easier to read, security checking and error handling are not included. This code example should not be used in a release configuration unless it has been modified to include them.

HRESULT hr;
// ICEMAPISession inherits IMAPISession, so we can use it in MAPILogonEx as long as we cast it to IMAPISession.
// Since we are using the Windows Mobile version, rather than the desktop version of MAPI, we should use ICEMAPISession.
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

How to Create a Messaging Application | Messaging Application Development for Windows Mobile-based Devices | Messaging | 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

Send Feedback on this topic to the authors

Feedback FAQs

© 2006 Microsoft Corporation. All rights reserved.