Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get an IMAP Flag from a Downloaded Email
See more IMAP Examples
Demonstrates the Chilkat Imap.GetMailFlag method, which returns the state of a named flag for an Email object that was downloaded from the server. The first argument is the Email and the second is the flag name. The return value is 1 if the flag is set, 0 if not, and -1 if the flag is not present. This example iterates a bundle and reports which messages are read vs unread.
Background: When Chilkat downloads a message it records the flags the server reported at fetch time, so you can inspect them locally with
GetMailFlag without another round trip. This is the client-side view: it reflects the state as of the download, not necessarily the live state on the server, which other clients could have changed. Use RefetchMailFlags to update an Email object's flags to the current server values.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;
msgSet: TMessageSet;
bundle: TEmailBundle;
email: TEmail;
i: Integer;
seen: Integer;
begin
success := False;
// Demonstrates the Imap.GetMailFlag method, which returns the state of a named flag for an
// Email object that was downloaded from the server. The 1st argument is the Email and the
// 2nd is the flag name. The return value is 1 if the flag is set, 0 if not, and -1 if the
// flag is not present.
imap := TImap.Create;
imap.Ssl := True;
imap.Port := 993;
success := imap.Connect('imap.example.com');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
success := imap.Login('user@example.com','myPassword');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
success := imap.SelectMailbox('Inbox');
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
msgSet := TMessageSet.Create;
success := imap.QueryMbx('ALL',True,msgSet);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
bundle := TEmailBundle.Create;
success := imap.FetchMsgSet(True,msgSet,bundle);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
email := TEmail.Create;
for i := 0 to bundle.MessageCount - 1 do
begin
success := bundle.EmailAt(i,email);
// Check whether each message has been read (the "Seen" flag).
seen := imap.GetMailFlag(email,'Seen');
if (seen = 1) then
begin
WriteLn(email.Subject + ' -- already read');
end;
ERROR: "}" expected
end;
imap.Free;
msgSet.Free;
bundle.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.