+1 vote

We are using Mail.dll components for downloading mails from POP3 mailbox. We are using the below code snippet for the purpose:

Pop3 pop3 = new POP3
pop3.ConnectSSL(serverAddress)
pop3.Login(username,Password)

AccountStats stat = pop3.GetAccountStat
For (int mailId = 1 ; mailId <= stat.MessageCount; mailId ++)
{
    String messageContent = pop3.GetMessageByNumber(mailId)
    //do Something with the Mail
    Pop3.DeleteMessageByNumber(mailId)  
}

Pop3.Close()

With the above logic, all the emails will be deleted from POP3 server, only after all the emails have been downloaded.

This at cases is not very convenient, say for e.g. We have lost connectivity to POP3 for a day & POP3 mailbox is full. The next day connection is restored and we start downloading mails.

Now no user will be able to send new emails(as Mailbox is full) unless we have finished downloading of all the emails (deleting the emails).

Is there a better approach which can be designed to avoid such issue. Could this be a solution: download one email and then delete the email from server (not just mark it for deletion)?

Please suggest the best solution which can be made using Mail.dll APIs. Thanks!

by

1 Answer

0 votes
 
Best answer

Unfortunately this is how POP3 protocol works.

Pop3.DeleteMessageByNumber issues DELE command which only marks message for deletion.

POP3 server doesn't actually delete the message until the QUIT command, issued by Pop3.Close method, is received.

This is not the issue with Mail.dll, but with how the protocol was designed.

There is no workaround other, than disconnecting after each delete, which is most likely not what you want to do because of performance penalty.

What I can suggest is:

  • Use IMAP protocol. It includes EXPUNGE command which forces the server to delete messages, which are marked for deletion. Mail.dll IMAP client issues this command after each delete.

  • Disconnect after processing some number of messages - not that efficient and still you can find yourself in a situation where connection is lost.

  • Use Pop3.GetAll method to get messages uids. Use Pop3.GetMessageByUID and Pop3.DeleteMessageByUID methods to download and delete emails.
    Remember all uids you have already processed - this way you can delete messages, that were processed, but are still returned by Pop3.GetAll.

The last solution depends a bit on how your POP3 server assigns uids to email messages.

Most servers generate truly unique uids for each message; uids don't change between sessions; and 2 different emails always have different uids.

However those assumptions are not imposed by POP3 protocol: Unique ID in POP3 protocol, so you'll have to check how your server is acting.

by (301k points)
edited by
...