Get the array of Unique-ID's of all messages in the inbox
Unique-ID (UID for short) is a unique string assigned to each message in the mailbox. Two messages in the mailbox can't have the same Unique-ID value.
POP3 object provides GetMessageUids method which returns zero-based array of Unique-IDs of all messages in the mailbox.
string uids = pop.GetMessageUids();
Dim uids() As String = pop.GetMessageUids()
Note: You should connect to the mail server and log in your account prior to using GetMessageUids method.
You can get UID value of any message as follows (note that indices are one-based, not zero-based):
Console.WriteLine("Unique-ID of message #5 is " + pop.GetMessageUidFromIndex(5));
Console.WriteLine("Unique-ID of message #5 is " + pop.GetMessageUidFromIndex(5))
To download (or delete) a message by its UID, you can use GetMessageIndexFromUid method to get message number which corresponds to the UID:
MailMessage msg = pop.DownloadMessageHeader(pop.GetMessageIndexFromUid(uid));
Dim msg As MailMessage = pop.DownloadMessageHeader(pop.GetMessageIndexFromUid(uid))
It's always recommended to use UIDs rather than just message numbers in all cases when you connect to the same account multiple times (for instance, if you connect to the server, get message list, then disconnect, allow the user to select the message in the list, and connect again to display the selected message). This is because message numbers can change while UIDs never change across sessions. This means if you know UID, you can be sure this UID will correspond to the same message when you connect to the account next time.
If you used just message number (let's say, message #3), you could get wrong message if someone deleted message #2 or message #1 AFTER you got the message list but BEFORE you displayed the message #3 to the user. This is because the former message #3 would have become message #2 after deletion of the former message #2, and the user would have received the next message in the list (the former message #4). Using UIDs prevents such weird behaviour.