mapi_sample1.c

This example shows a very basic MAPI program, including setup, login, and teardown.

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;
talloc_free(mem_ctx);
return 0;
}
_PUBLIC_ enum MAPISTATUS GetDefaultProfile(struct mapi_context *mapi_ctx, char **profname)
Definition IProfAdmin.c:1315
_PUBLIC_ enum MAPISTATUS GetLastError(void)
Definition IUnknown.c:176
_PUBLIC_ enum MAPISTATUS MAPIInitialize(struct mapi_context **_mapi_ctx, const char *profiledb)
Definition cdo_mapi.c:221
_PUBLIC_ void MAPIUninitialize(struct mapi_context *mapi_ctx)
Definition cdo_mapi.c:272
_PUBLIC_ enum MAPISTATUS MapiLogonEx(struct mapi_context *mapi_ctx, struct mapi_session **session, const char *profname, const char *password)
Definition cdo_mapi.c:59
void mapi_errstr(const char *, enum MAPISTATUS)
Definition mapicode.c:292
Definition mapi_context.h:31
Definition mapi_provider.h:47
struct mapi_context * mapi_ctx
Definition mapi_provider.h:53

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/