Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Clear an Email Object
See more Email Object Examples
Demonstrates the Chilkat Email.Clear method, which removes the current message content — recipients, headers, body representations, attachments, related items, and embedded messages — leaving an empty email object. This example builds a message with a recipient and attachment, clears it, and prints the counts before and after.
Background: Reusing a single
Email object across many operations is efficient, but leftover state from a previous message could leak into the next. Clear resets the object completely, giving you a clean slate — the safe way to start a fresh message without allocating a new object.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.Email;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
email: TEmail;
begin
// Demonstrates the Clear method, which removes the current message content, including
// recipients, headers, body representations, attachments, related items, and embedded
// messages -- leaving an empty email object.
email := TEmail.Create;
email.Subject := 'A message';
email.Body := 'Some body text.';
email.AddTo('Bob','bob@example.com');
email.AddStringAttachment('notes.txt','Some notes.');
WriteLn('NumTo before clear = ' + email.NumTo);
WriteLn('NumAttachments before clear = ' + email.NumAttachments);
// Remove all content, resetting the email object.
email.Clear();
WriteLn('NumTo after clear = ' + email.NumTo);
WriteLn('NumAttachments after clear = ' + email.NumAttachments);
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.