Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Scan for Emails with Attachments and Save Attachments to Files
Scan for emails with attachments and save attachments.Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.EmailBundle,
Chilkat.Imap,
Chilkat.Email,
Chilkat.MessageSet;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
fetchUids: Boolean;
messageSet: TMessageSet;
bundle: TEmailBundle;
headersOnly: Boolean;
fullEmail: TEmail;
emailHeader: TEmail;
i: Integer;
numAttach: Integer;
uidStr: string;
uid: Integer;
j: Integer;
filename: string;
begin
success := False;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
imap := TImap.Create;
// Connect to an IMAP server.
// Use TLS
imap.Ssl := True;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Login
success := imap.Login('myLogin','myPassword');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Select an IMAP mailbox
success := imap.SelectMailbox('Inbox');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// We can choose to fetch UIDs or sequence numbers.
fetchUids := True;
// Get the message IDs of all the emails in the mailbox
messageSet := TMessageSet.Create;
success := imap.QueryMbx('ALL',fetchUids,messageSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Fetch the email headers into a bundle object:
bundle := TEmailBundle.Create;
headersOnly := True;
success := imap.FetchMsgSet(headersOnly,messageSet,bundle);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Scan for emails with attachments, and save the attachments
// to a sub-directory.
fullEmail := TEmail.Create;
emailHeader := TEmail.Create;
i := 0;
while i < bundle.MessageCount do
begin
// The bundle contains email headers..
bundle.EmailAt(i,emailHeader);
// Does this email have attachments?
// Use GetMailNumAttach because the attachments
// are not actually in the email object because
// we only downloaded headers.
numAttach := imap.GetMailNumAttach(emailHeader);
if (numAttach > 0) then
begin
// Download the entire email and save the
// attachments. (Remember, we
// need to download the entire email because
// only the headers were previously downloaded.
// The ckx-imap-uid header field is added when
// headers are downloaded. This makes it possible
// to get the UID from the email object.
uidStr := emailHeader.GetHeaderField('ckx-imap-uid');
uid := StrToInt(uidStr);
success := imap.FetchEmail(False,uid,True,fullEmail);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
success := fullEmail.SaveAllAttachments('attachmentsDir');
for j := 0 to numAttach - 1 do
begin
filename := imap.GetMailAttachFilename(emailHeader,j);
WriteLn(filename);
end;
end;
i := i + 1;
end;
// Disconnect from the IMAP server.
success := imap.Disconnect();
imap.Free;
messageSet.Free;
bundle.Free;
fullEmail.Free;
emailHeader.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.