Product SiteDocumentation Site

4.4. Using tables: listing messages

Most of the data inside MAPI can be viewed by using MAPI tables. MAPI tables are just what you would expect: a column/row view of a set of data. In this case, the table we want is the table of e-mail messages in the inbox. Each message in the inbox is one row in the table, and the columns are the properties of each message, for example the subject and the data.
The best way to think of the inbox’s table is exactly the way that it is shown in your e-mail client: all the emails vertically and various properties of the emails horizontally.
Tables in MAPI are very flexible and allow setting different columns, they have a row cursor, allow reading arbitrary numbers of rows, support sorting and even grouping.
We can use a MAPI table to read the messages in the inbox (in fact, this is the only way to get the list of messages in the inbox):
table = inbox.GetContentsTable(0)
table.SetColumns([PR_ENTRYID, PR_SUBJECT], 0)
table.SortTable(SSortOrderSet( [ SSort(PR_SUBJECT, TABLE_SORT_ASCEND) ], 0, 0), 0);
rows = table.QueryRows(10, 0)
This needs some more explanation. The first line calls GetContentsTable() to get the table associated with the inbox. The SetColumns() call then selects two columns that we are interested in: PR_ENTRYID (the unique ID of each message) and PR_SUBJECT (the subject of each message). We could have requested much more properties, but requesting more properties is slower because it needs more bandwidth, i/o, cache, etc. from the server.
We then sort the table by creating an SSortOrderSet. The SSortOrderSet constructor accepts a list of the columns to sort by (note that the columns used for sorted are not necessarily in the viewable column set set by SetColumns()). Each property can be sorted ascending (TABLE_SORT_ASCEND) or descending (TABLE_SORT_DESCEND). The two zero’s passed to the SSortOrderSet constructor are how many columns you wish to use for grouping, and how many of those groups should be expanded, but we’re not using that right now.
The last line actually requests the data from the server. The QueryRows() call requests 10 rows. Since we just openen the table, it will give us the first 10 rows. A subsequent call to QueryRows() will give us the next 10 rows, etc.
The rows are returned as a two-dimensional array: an array of arrays of SPropValues. Processing them gives you an idea of what it looks like:
for row in rows:
  print row[1].Value + '\n'
It is important to note that the order of the properties in the row output is always exactly equal to the order of properties passed to SetColumns(). In this case we know that row[1] is the PR_SUBJECT property, and we can therefore print it in the way described above.