How to: End a MAPI Session

When the user is done performing messaging tasks, there is no further need to consume valuable system resources with the messaging subsystem. At this point, it is a good idea to free resources by ending the MAPI session.

To end the MAPI session

  1. End the MAPI session with the IMAPISession::Logoff method, as follows:

    hr = pSession->Logoff(0, 0, 0);
    
  2. Decrement the MAPI session reference count by calling IUnknown::Release on the MAPI session object:

    pSession->Release();
    
  3. Decrement the MAPI subsystem reference count, and delete any per-instance global data for the MAPI DLL with the MAPIUninitialize global MAPI function:

    MAPIUninitialize();
    

Example

The following code demonstrates how to end a MAPI session.

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

pSession->Release();
pSession = NULL;
MAPIUninitialize();

See Also

Messaging

Messaging Overview

How to: Begin a MAPI Session

How to: Connect to a Message Store

How to: Create a Message

How to: Send a Message

Messaging Sample Code

Send feedback on this topic to the authors.

© 2005 Microsoft Corporation. All rights reserved.