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.
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.
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.
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)
{
return;
}
UINT *pSeqList = new UINT[nMessageCount];
UINT *pUidList = new UINT[nMessageCount];
INT nUidCount = imapClient.EnumMessageUids(pSeqList, pUidList, nMessageCount);
if (nUidCount > 0 && nUidCount <= nMessageCount)
{
for (INT nIndex = 0; nIndex < nUidCount; nIndex++)
{
_tprintf(_T("\tMessage %u was assigned UID %u\n"), pSeqList[nIndex], pUidList[nIndex]);
}
}
else
{
DWORD dwError = imapClient.GetLastError();
_tprintf(_T("Unable to enumerate message UIDs (error code: 0x%lx)\n"), dwError);
}
delete[] pSeqList;
delete[] pUidList;