Delphi DLL
Delphi DLL
IMAP Delete Old Email (before a specified date)
See more IMAP Examples
Demonstrates how to delete email older than a particular date.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Imap, Email, MessageSet;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
imap: HCkImap;
fetchUids: Boolean;
messageSet: HCkMessageSet;
email: HCkEmail;
i: Integer;
count: Integer;
headerOnly: Boolean;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap := CkImap_Create();
// Connect to an IMAP server.
// Use TLS
CkImap_putSsl(imap,True);
CkImap_putPort(imap,993);
// Use your IMAP server domain. This example
success := CkImap_Connect(imap,'imap.example.com');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Login
success := CkImap_Login(imap,'myLogin','myPassword');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Select an IMAP mailbox
success := CkImap_SelectMailbox(imap,'Inbox');
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Get message ID's for emails older than 1/1/2025
fetchUids := True;
messageSet := CkMessageSet_Create();
success := CkImap_QueryMbx(imap,'SENTBEFORE 01-Jan-2025',fetchUids,messageSet);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// If desired, we can examine the Subject of each email to be deleted..
email := CkEmail_Create();
i := 0;
count := CkMessageSet_getCount(messageSet);
headerOnly := True;
while i < count do
begin
success := CkImap_FetchEmail(imap,headerOnly,CkMessageSet_GetId(messageSet,i),True,email);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
Memo1.Lines.Add(CkEmail__localDateStr(email));
Memo1.Lines.Add(CkEmail__subject(email));
i := i + 1;
end;
// Set the Deleted flag for each message:
success := CkImap_SetFlags(imap,messageSet,'Deleted',1);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Expunge and close the mailbox.
success := CkImap_ExpungeAndClose(imap);
if (success = False) then
begin
Memo1.Lines.Add(CkImap__lastErrorText(imap));
Exit;
end;
// Disconnect from the IMAP server.
success := CkImap_Disconnect(imap);
CkImap_Dispose(imap);
CkMessageSet_Dispose(messageSet);
CkEmail_Dispose(email);
end;