Partager via


Beginning a MAPI Session

4/8/2010

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. Since you are using the Windows Mobile version of MAPI and not the desktop version—declare your session object as type ICEMAPISession, and not type IMAPISession. Since ICEMAPISession inherits from IMAPISession, you can use a pointer to an object of this type in MAPILogonEx, but you must cast it to the type IMAPISession.

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

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 * pSession = NULL;

hr = MAPIInitialize(NULL);

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

The Windows Mobile Professional SDK , from this Microsoft Web site, and Windows Mobile Standard SDK, from this Microsoft Web site, ship with a code sample called "Sending a Message," which uses the CEMAPI API. For more information, see Sending a Message.

Compiling the Code

To compile the code sample, you must add references to the CE MAPI type library and the CE MAPI header files, to your project.

  1. Add preprocessor directives that include the header files in with the rest of your project files when you build your project. You do this by typing the following statements at the top of either your main C++ source file or your header file.

    #include <cemapi.h>
    #include <mapidefs.h>
    #include <mapiutil.h>
    #include <mapix.h>
    
  2. Specify a linker dependency to the CE MAPI type library as a project property.

    1. Press Alt+F7, and the Project Property Pages dialog box appears.
    2. In the dialog box, navigate to the Input property page for the Linker (navigate to Configuration Properties > Linker > Input).
    3. In Additional Dependencies, type cemapi.lib, and then click OK.
  3. Rebuild the Solution (press Ctrl+Alt+F7).

See Also

Tasks

Connecting to a Message Store
Creating a Message
Sending a Message
Ending a MAPI Session

Concepts

Messaging Samples

Other Resources

Messaging Common Tasks
Messaging Application Development
Messaging