Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Set an IMAP Flag Using an Email Object
See more IMAP Examples
Demonstrates the Chilkat Imap.SetMailFlag method, which sets or clears a flag on the server for the message represented by an Email object. The first argument is the Email, the second is the flag name, and the third is the value (1 to set, 0 to clear). This example flags each fetched message as Flagged on the server.
Background:
SetMailFlag is convenient when you already hold an Email from a fetch: Chilkat uses the UID it recorded on that object to target the right message on the server, so you do not have to track IDs yourself. It updates both the server and the local Email object, keeping the two in sync. Under the hood this is the same STORE operation as SetFlag — just addressed by object rather than by raw ID.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;
begin
success := False;
// Demonstrates the Imap.SetMailFlag method, which sets or clears a flag on the server for the
// message represented by an Email object. The 1st argument is the Email, the 2nd is the flag
// name, and the 3rd is the value (1 to set, 0 to clear).
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('UNSEEN',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);
// Mark this message as Flagged on the server. A value of 1 sets the flag; 0 would clear it.
success := imap.SetMailFlag(email,'Flagged',1);
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
end;
end;
WriteLn('Flagged ' + bundle.MessageCount + ' messages.');
success := imap.Disconnect();
if (success = False) then
begin
WriteLn(imap.LastErrorText);
Exit;
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.