mapi_sample1.c

This example shows a very basic MAPI program, including setup, login, and teardown.The first MAPI library function called is MAPIInitialize(). As its name suggests, this function initializes MAPI library: It creates the MAPI global context, opens the profile database store (database path passed as function parameter) and initialize Samba4 transport layer. This function must be called prior any other MAPI library operations.

Once MAPI is initialized, we need to create a connection to Exchange and open MAPI sessions with the user credentials. This is the purpose of MapiLogonEx() which needs to be executed prior doing any effective code. This function takes a pointer on a mapi_session structure, the profile username and an optional password in case you decided to store it outside the profile. In the example above, we retrieve the default profile name from the database using GetDefaultProfile() Note that MapiLogonEx() opens connections to both the EMSMDB and EMSABP store providers. If you intend to interact with a single provider, use MapiLogonProvider() instead.

Finally we call MAPIUninitialize() prior to leaving the function. This opaque function will clean up the memory allocated during the session and stored within the global MAPI context.

#define DEFAULT_PROFDB_PATH "%s/.openchange/profiles.ldb"
int main(int argc, char *argv[])
{
TALLOC_CTX *mem_ctx;
struct mapi_context *mapi_ctx;
enum MAPISTATUS retval;
struct mapi_session *session = NULL;
char *profdb;
char *profname;
mem_ctx = talloc_named(NULL, 0, "mapi_sample1");
profdb = talloc_asprintf(mem_ctx, DEFAULT_PROFDB_PATH, getenv("HOME"));
retval = MAPIInitialize(&mapi_ctx, profdb);
mapi_errstr("MAPIInitialize", GetLastError());
if (retval != MAPI_E_SUCCESS) return -1;
retval = GetDefaultProfile(mapi_ctx, &profname);
mapi_errstr("GetDefaultProfile", GetLastError());
if (retval != MAPI_E_SUCCESS) return -1;
retval = MapiLogonEx(mapi_ctx, &session, profname, NULL);
mapi_errstr("MapiLogonEx", GetLastError());
if (retval != MAPI_E_SUCCESS) return -1;
MAPIUninitialize(mapi_ctx);
talloc_free(mem_ctx);
return 0;
}

Creative Commons License
Creative Commons Attribution icon Creative Commons Share Alike icon
This content is licensed under the Creative Commons
Attribution ShareAlike License v. 3.0:
http://creativecommons.org/licenses/by-sa/3.0/