Site Map Contact Us Home
E-mail Newsletter
Subscribe to get informed about
Clever Components news.

Your Name:
Your Email:
 
SUBSCRIBE
 
Previous Newsletters
 




Products Articles Downloads Order Support
Customer Portal      

Cleaning email account from unneeded Emails on a server

Submitted on March 17, 2004

Abstract

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 support@clevercomponents.com) 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.

Algorithm

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:

procedure TMainForm.CollectMsgInfo();
var
   i: Integer;
begin
   lbMessages.Items.Clear();
   FMessageCount := clPOP3.MessageCount;
   for i := FMessageCount - 1 downto 0 do
   begin
      clPOP3.RetrieveHeader(i);
      if (Round(Date()) - Round(clMessageParser.Date) + 1) < StrToIntDef(cbDays.Items[cbDays.ItemIndex], 0) then
      begin
         lbMessages.Items.AddObject(clMessageParser.Subject + '; ' + DateTimeToStr(clMessageParser.Date), Pointer(i));
      end;
   end;
end;

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.

procedure TMainForm.CleanMessages();
var
   i: Integer;
begin
   for i := FMessageCount - 1 downto 0 do
   begin
      if lbMessages.Items.IndexOfObject(Pointer(i)) < 0 then
         clPOP3.Delete(i);
   end;
end;

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.

Source Code

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,
Sergey Shirokov 
Clever Components team.

    Copyright © 2000-2024