Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Fetch Single Email by UID or Sequence Number
Assuming the UID is known, download a single email by UID from an IMAP mail server.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.Imap,
Chilkat.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
imap: TImap;
email: TEmail;
uid: Integer;
isUid: Boolean;
j: Integer;
numAttach: Integer;
attachSize: Integer;
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('***','***');
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;
email := TEmail.Create;
uid := 2014;
isUid := True;
success := imap.FetchEmail(False,uid,isUid,email);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
// Display the From and Subject
WriteLn(email.FromAddress);
WriteLn(email.Subject);
// Display the Body property, which is the default body.
// If an email has an HTML body, the Body property contains
// the HTML source. Otherwise it contains the plain-text
// body.
WriteLn('---- EMAIL BODY ----');
WriteLn(email.Body);
WriteLn('--------------------');
// Display the recipients:
for j := 0 to email.NumTo - 1 do
begin
WriteLn(email.GetToName(j) + ', ' + email.GetToAddr(j));
end;
for j := 0 to email.NumCC - 1 do
begin
WriteLn(email.GetCcName(j) + ', ' + email.GetCcAddr(j));
end;
// Show the total size of the email, including body and attachments:
WriteLn(email.Size);
// When a full email is downloaded, we would use the
// email.NumAttachments property in conjunction with the
// email.GetMailAttach* methods.
// However, when an email object contains only the header,
// we need to access the attachment info differently:
numAttach := imap.GetMailNumAttach(email);
WriteLn(numAttach);
for j := 0 to numAttach - 1 do
begin
WriteLn(imap.GetMailAttachFilename(email,j));
attachSize := imap.GetMailAttachSize(email,j);
WriteLn(' size = ' + attachSize + ' bytes');
end;
WriteLn('--');
// Disconnect from the IMAP server.
success := imap.Disconnect();
imap.Free;
email.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.