Search code examples
javaemailsocketspop3email-client

How do email clients handle mails from the POP server?


When writing a connection to connect to the POP3 protocol through sockets, I could use the LIST command to get the list of emails. Suppose I do a LIST command, it returns 3700 mails in the list. I read the whole response into a buffer. Now, I want to list these mails one by one on my application.

LIST
1 1472
...
3696 3224
3697 5998
3698 1970
3699 1425
3700 129345
.

I had 2 ideas in mind. First, I could parse the response line by line and get the message number of the message. And for each line I get the message number, say #3700, I do a TOP 3700 10. So this would read 10 lines of the mail headers. At first, I thought this would be good because I don't have to do a download the entire mail just for the subject, name, from address and some bit of brief content of the email; I could save the traffic. But the problem is not all emails have only 10 lines of headers. Some emails have extremely long headers while others are very short. It is difficult to determine how many lines to read. Not only that, because the email is not entirely downloaded, when the user wants to read the full mail, I have to send another RETR command to get the whole email. Then suppose if there were another connection to the POP account and deleted the message #3700. My application still recognise the email by this "non-unique" message id. When my application wants to download the email and sends a RETR 3700, it will respond with a -ERR Server Unavailable. 21

The second idea I have is then to use a RETR 3700. This would read the whole mail, just for that few information to list it in the application. I thought this is kind of silly because if the mailbox has a lot of mails, I am retrieving the entire mailbox into the application's memory!

What are the common/clever ways an email client would handle the mails from the POP server?

PS: I am not using the JavaMail because the intention is to learn more about how the POP protocol works and the implementations used to work with the protocols.


Solution

  • The 2nd argument of TOP command is a non-negative number of message lines to download - header lines are not included.

    You can use TOP command with 0 as line count (TOP message_number 0) to download only message headers.