fetchmail.c

This example shows how to fetch mail from the server.We initialize MAPI library with the profiles database path, retrieve the default profile name and open connections to both the Exchange message store provider (EMSMDB) and Exchange Address Book provider (EMSABP).

Open the message store

Now we have opened a connection to the Exchange message store provider, we can open the user mailbox store with OpenMsgStore() This function will return a set of pre-defined folder unique IDs (stored on double values) and a pointer to the upper object we can access in MAPI hierarchy.

Opening the Inbox folder

We now open the Inbox folder. Since OpenMsgStore() returns a set of common folders identifiers we store in the message store object (obj_store), we can retrieve them using the convenient GetDefaultFolder() function. This function doesn't generate any network traffic, but returns the folder identifier associated with the constant passed as argument (here olFolderInbox).

We could have used MAPI tables and GetHierarchyTable() function to find Inbox folder identifier. We would have had to retrieve the Top Information Store hierarchy table, customize the view with PR_FID (Folder ID MAPI property) and PR_DISPLAY_NAME, find the IPM_SUBTREE folder identifier, open it, retrieve the Hierarchy Table, call SetColumns() with PR_FID and finally browse table rows until we found the Inbox folder.folders to store emails within IPM_SUBTREE folder hierarchy.

Retrieve contents table

Once the Inbox folder is opened, we can call GetContentsTable() to create the view needed to list all the children objects. In the current example we suppose we will only retrieve IPM.Post objects (emails).

Customizing the MAPI view

We now customize the MAPI view and set the columns with the property tags we want to access: PR_FID (Folder Identifier) and PR_MID (Message identifier). MAPI uses unique and permanent identifiers to classify objects. These identifiers are double values (8 bytes) and never change until you move the object to another location.

We now enter the last step of the fetching process:

  • Call QueryPosition() to retrieve the current cursor position in the contents table. The function returns the approximate fractional position with a Numerator and Denominator. Denominator is the total number of rows in the table.
  • Recursively call QueryRows() with the TBL_ADVANCE flag to fetch table rows.
  • Iterate through QueryRows results
  • Retrieve columns values for each row with the convenient find_SPropValue_data()
  • Open the message given its folder and message ids.
  • Call GetPropsAll() rather than GetProps() to retrieve all properties associated with a given object
  • Call one of OpenChange mapidump API function to display nice messages dump on standard output.

We finally release mapi objects and clean up the MAPI library before returning

#define DEFAULT_PROFDB "%s/.openchange/profiles.ldb"
int main(int argc, char *argv[])
{
enum MAPISTATUS retval;
struct mapi_context *mapi_ctx;
TALLOC_CTX *mem_ctx;
struct mapi_session *session = NULL;
mapi_object_t obj_store;
mapi_object_t obj_folder;
mapi_object_t obj_table;
mapi_object_t obj_message;
struct mapi_SPropValue_array props_all;
struct SRowSet rowset;
struct SPropTagArray *SPropTagArray;
mapi_id_t id_inbox;
mapi_id_t *fid, *mid;
char *profname;
char *profdb;
uint32_t Numerator;
uint32_t Denominator;
uint32_t i;
mem_ctx = talloc_named(NULL, 0, "fetchmail");
/* Initialize MAPI */
profdb = talloc_asprintf(mem_ctx, DEFAULT_PROFDB, getenv("HOME"));
retval = MAPIInitialize(&mapi_ctx, profdb);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
/* Find Default Profile */
retval = GetDefaultProfile(mapi_ctx, &profname);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
/* Log on EMSMDB and NSPI */
retval = MapiLogonEx(mapi_ctx, &session, profname, NULL);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
/* Open Message Store */
mapi_object_init(&obj_store);
retval = OpenMsgStore(session, &obj_store);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
/* Find Inbox default folder */
retval = GetDefaultFolder(&obj_store, &id_inbox, olFolderInbox);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
/* Open Inbox folder */
mapi_object_init(&obj_folder);
retval = OpenFolder(&obj_store, id_inbox, &obj_folder);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
/* Retrieve Inbox content table */
mapi_object_init(&obj_table);
retval = GetContentsTable(&obj_folder, &obj_table, 0x0, NULL);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
/* Create the MAPI table view */
SPropTagArray = set_SPropTagArray(mem_ctx, 0x2, PR_FID, PR_MID);
retval = SetColumns(&obj_table, SPropTagArray);
MAPIFreeBuffer(SPropTagArray);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
talloc_free(mem_ctx);
/* Get current cursor position */
retval = QueryPosition(&obj_table, &Numerator, &Denominator);
MAPI_RETVAL_IF(retval, retval, mem_ctx);
/* Iterate through rows */
while ((retval = QueryRows(&obj_table, Denominator, TBL_ADVANCE, &rowset))
!= -1 && rowset.cRows) {
for (i = 0; i < rowset.cRows; i++) {
fid = (mapi_id_t *)find_SPropValue_data(&(rowset.aRow[i]), PR_FID);
mid = (mapi_id_t *)find_SPropValue_data(&(rowset.aRow[i]), PR_MID);
mapi_object_init(&obj_message);
retval = OpenMessage(&obj_store, *fid, *mid, &obj_message, 0x0);
if (retval != MAPI_E_NOT_FOUND) {
retval = GetPropsAll(&obj_message, MAPI_UNICODE, &props_all);
mapidump_message(&props_all, NULL, &obj_message);
mapi_object_release(&obj_message);
}
}
}
/* Release MAPI objects */
mapi_object_release(&obj_table);
mapi_object_release(&obj_folder);
Logoff(&obj_store);
/* Uninitialize MAPI */
MAPIUninitialize(mapi_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/