Knowledge Base

How to download unread e-mails in POP3?

To download only new messages in a POP3 account:

1. You'll need a database where you'll store the list of UIDs of message which have already been downloaded during previous sessions. Let's assume you already have one.

2. Download list of UIDs of all message in Inbox using pop.GetMessageUids method:

http://www.afterlogic.com/mailbee-net/docs/#MailBee.Pop3Mail.Pop3.GetMessageUids.html

3. Compare the list of UIDs you've downloaded with the list in your database. All UIDs which are present in the new list but are missing in the list you got from the database correspond to unread messages. For each of new UIDs, convert its UID to message index using GetMessageIndexFromUid method:

http://www.afterlogic.com/mailbee-net/docs/#MailBee.Pop3Mail.Pop3.GetMessageIndexFromUid.html

4. Now when you have MessageIndex converted from UID, you can download message using DownloadEntireMessage method:

http://www.afterlogic.com/mailbee-net/docs/#MailBee.Pop3Mail.Pop3.DownloadEntireMessage.html

This should be repeated for every new message.

5. After downloading all new messages, replace the list of UIDs in the database with the list you've downloaded (i.e. delete all records with older UIDs for this email account and save into database all UIDs you received from the mail server during the current session). It's important that you should save into the database the list of all UIDs (as returned by pop.GetMessageUids method), not only UIDs of new messages.

When you run this for the very first time (and your UID list in database is empty), all UIDs will be considered new and you'll get all the messages.

This approach is used by virtually any POP3 client.