Reading Mail

Three API functions are related to reading messages. Each one handles a different aspect of the process, and all three must be used to retrieve incoming messages successfully. These functions are:


Iterates through the messages in the user’s Inbox, returning a message ID for each message.


Fetches a message to the local machine, given the message ID returned from MAPIFindNext.


Reads a fetched message.

The code in Example 3-4 loops through the messages in the user’s Inbox and prints the subject text of each message to the Visual Basic Immediate window. This code assumes that a session has already been established via a call to the MAPILogon function, and that the session handle is held in a variable called nMAPISession.

Example 3-4. Reading Messages

Dim nRetVal As Long Dim nRetValFindNext As Long Dim strSeedMessageID As String Dim strMessageID As String Dim nMsg As Long Dim MyMessage As MAPIMessage ReDim MyRecips(0 To 0) As MapiRecip ReDim MyFiles(0 To 0) As MapiFile Dim recipOriginator As MapiRecip Dim nRecipients As Long Dim nFiles As Long ' This tells MAPIFindNext to start with the first message. strSeedMessageID = "" Do ' for each message in the user's Inbox ' Get the next message ID. strMessageID = String(512, 0) nRetValFindNext = MAPIFindNext(nMAPISession, 0, "", _ strSeedMessageID, 0, 0, strMessageID) ' if there was another message If nRetValFindNext = SUCCESS_SUCCESS Then ' Fetch message associated with the message ID. nRetVal ...

Get CDO & MAPI Programming with Visual Basic: now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.