Cleaning email account from unneeded Emails on a server
Submitted on March 17, 2004
From time to time we need to delete some old emails on mail server. In the company where I work we have to share some corporative accounts (such as firstname.lastname@example.org) between different persons. For this we need to set up our email accounts to leave a copy of the message on the server. As a result there are hundreds of messages including spam (yes, it is inevitable) left on our mail account on the server. It is not always convenient to access to the mail server drop directory directly and delete all the messages manually. For example, I am connecting remotely and there is no terminal service (or another remote tool) present. Another reason I should know the age of the letter in order to decide if I should delete it or not - we always should leave the last one or two-day letters to be received by other team members.
So I decided to solve this task, which is not complicated by writing a simple app that can list mail messages, filter them by date and finally delete unnecessary ones.
As the socket engine I choose the POP3 component from Internet Components - Clever Internet Suite, but it is quite possible to write such an application using any library which provides the ability to operate the mail server via POP3 protocol and parse MIME formatted messages.
Functionally, the program consists of two parts: collecting the information about email messages and deleting email messages by filtering according to the mail date.
We enumerate email messages available on the server, parse the header parts of them and then compare the message Date field on matching our filter:
We store the message numbers which cannot be deleted within the Objects collection of the ListBox items.
Next step is walking through the email messages and deleting each of them except for the messages the numbers of which have been stored within the Objects ListBox collection.
Storing the number of messages within the class member FMessageCount is recommended, because while collecting the messages info new messages may arrive. These new messages are not enumerated within the first step of the algorithm and potentially may be deleted on the final step.
The full source code of classes being used in this article can be downloaded at MailCleaner.zip
This code is constantly being improved and your comments and suggestions are always welcome.
With best regards,