CImapClient::EnumMessageUids Method  
 
INT EnumMessageUids(
  UINT nFirstMessageId,  
  UINT nLastMessageId,  
  DWORD dwMessageFlags,  
  UINT *lpSequenceIdList,  
  UINT *lpMessageUidList  
  INT nMaxMessages  
);
INT EnumMessageUids(
  UINT *lpSequenceIdList,  
  DWORD *lpMessageUidList  
  INT nMaxMessages  
);

The EnumMessageUids method populates an array of message sequence IDs and their corresponding message UIDs.

Parameters

nFirstMessageId
An unsigned integer value which specifies the first message to enumerate. This value must be greater than zero and specify a valid message identifier. The first message in the mailbox has a value of one.
nLastMessageId
An unsigned integer value which specifies the last message to enumerate. This value must be greater than or equal to the value of the nFirstMessageId parameter. A special value of 0xFFFFFFFF (-1) can be used to specify the last message in the mailbox.
dwMessageFlags
This parameter is used to determine which messages are enumerated. If the value is zero, then all messages in the mailbox will be enumerated. If the value is non-zero, only those messages which have at least one of the specified flags will be returned. The following flags can be used to filter which messages are included in the result:
Value Description
IMAP_FLAG_ANSWERED Return only those messages which have been answered.
IMAP_FLAG_DELETED Return only those messages which have been marked for deletion.
IMAP_FLAG_DRAFT Return only those messages which have been marked as draft copies.
IMAP_FLAG_URGENT Return only those messages which have been flagged for urgent or special attention.
IMAP_FLAG_RECENT Return only those messages which have been recently added to the mailbox.
IMAP_FLAG_SEEN Return only those messages which have been read.
lpSequenceIdList
A pointer to an array of unsigned integers that will receive the message sequence numbers for each enumerated message. The message sequence numbers are what most methods use to specify a message in the current mailbox. These values should never be stored by your application. This parameter must not be NULL.
lpMessageUidList
A pointer to an array of unsigned integers that will receive the message UIDs corresponding to the messages in the sequence. The message UIDs are unique values which persist across multiple client sessions. These values can be stored by your application and then mapped to their corresponding sequential message ID. This parameter must not be NULL.
nMaxMessages
An integer value which specifies the maximum size of both arrays. This value cannot be zero.

Return Value

If the method succeeds, the return value is the number of entities populated in both the lpSequenceIdList and lpMessageUidList arrays. If no messages match the specified criteria, the method will return a value of zero. If an error is encountered, the method returns IMAP_ERROR. To get extended error information, call the GetLastError method.

Remarks

The EnumMessageUids method retrieves a list of messages in the specified range and populates parallel arrays with each message's sequence number and corresponding unique identifier (UID). This enables applications to correlate transient message numbers with persistent UIDs for more reliable tracking across sessions. The lpSequenceIdList and lpMessageUidList arrays must be the same size, and both must be large enough to store the number of values specified by the nMaxMessages parameter. Both the sequential and unique message IDs are unsigned integer values, but they cannot be used interchangeably. Note that methods such as GetMessage and DeleteMessage expect sequential message numbers, not message UIDs.

If your application stores the message UIDs to maintain some persistent information about the mailbox, this method will enable you to lookup those message UIDs and find the corresponding sequential message number. In most cases, you would want to call this method immediately after calling SelectMailbox. This will cause the client to request a list of message UIDs in the selected mailbox within the range that you specify. Mailboxes with a very large number of messages may cause the application to block for an extended period of time while the list is returned and processed.

To map all messages in the selected mailbox, set nFirstMessageId = 1, nLastMessageId = (UINT)-1 and dwMessageFlags = 0.

If the message UID is being stored locally by the client to identify the message over multiple sessions, it must also store the mailbox UID. Only the combination of the mailbox name, mailbox UID and message UID can be used to uniquely identify a given message on the server. Although IMAP server implementations are encouraged to maintain persistent message UIDs, they are not required to do so. Message UIDs may change if the mailbox is recreated or its unique identifier (UIDVALIDITY) is reset.

In some cases, particularly with newly created or infrequently used mailboxes, the message sequence numbers may coincidentally match the UIDs assigned by the server. For example, the first message in a new mailbox might have both a message sequence number and UID of 1. However, this is not guaranteed, and applications should never assume that sequence numbers and UIDs are interchangeable. Sequence numbers are dynamic and reflect the current ordering of messages in the selected mailbox. They can change between sessions or when messages are added or deleted. UIDs, on the other hand, are intended to be persistent identifiers that remain associated with a specific message, even if its sequence number changes.

It is important to note that a message UID is not the same as the Message-ID header field found within the message content. The UID is a unique numeric identifier assigned by the IMAP server to a message in a specific mailbox. It is used by the protocol to track messages even as their sequence numbers change due to mailbox activity (e.g.: new messages arriving or messages being deleted). In contrast, the Message-ID header is a string value generated by the sending email client when the message is created. It is part of the message headers and intended to be globally unique for email threading and deduplication purposes, but it has no direct relationship to the UID used by the server.

Example

This example demonstrates how to enumerate all messages in the currently selected mailbox and retrieve the mapping between their sequential message numbers and server-assigned UIDs. This is useful when your application needs to track messages persistently across sessions using UIDs, while still interacting with the API methods that require sequence numbers such as GetMessage.

// Get some information about the currently selected mailbox
UINT nLastMessage = 0;
DWORD dwMailboxSize = 0;
INT nMessageCount = imapClient.GetMessageCount(&nLastMessage, &dwMailboxSize);

_tprintf(_T("There are %d messages in %s (last message is %u, mailbox size is %lu bytes)\n"),
         nMessageCount, szMailbox, nLastMessage, dwMailboxSize);

if (nMessageCount < 1)
{
    // There are no messages in the current mailbox
    return;
}

// Allocate memory for the array of sequence numbers and UIDs
UINT *pSeqList = new UINT[nMessageCount];
UINT *pUidList = new UINT[nMessageCount];

// Populate the arrays with message sequence numbers and corresponding UIDs
INT nUidCount = imapClient.EnumMessageUids(pSeqList, pUidList, nMessageCount);

if (nUidCount > 0 && nUidCount <= nMessageCount)
{
    // Display the mapping of message sequence numbers to UIDs
    for (INT nIndex = 0; nIndex < nUidCount; nIndex++)
    {
        _tprintf(_T("\tMessage %u was assigned UID %u\n"), pSeqList[nIndex], pUidList[nIndex]);
    }
}
else
{
    // Enumeration failed or no matching messages
    DWORD dwError = imapClient.GetLastError();
    _tprintf(_T("Unable to enumerate message UIDs (error code: 0x%lx)\n"), dwError);
}

// Free the memory allocated for our arrays
delete[] pSeqList;
delete[] pUidList;

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: csmapv12.lib

See Also