Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get the Size of an Attachment
See more Email Object Examples
Demonstrates the Chilkat Email.GetAttachmentSize method, which returns the size in bytes of the attachment at a given zero-based index, or -1 if the index does not identify an existing attachment. This example adds an attachment and prints its size.
Background: The size reported is that of the attachment's actual (decoded) data, not its larger Base64-encoded form on the wire. Knowing per-attachment sizes is handy for enforcing limits, showing a size next to each attachment in a UI, or deciding whether to save or skip a large part. The
-1 sentinel is how the method signals an out-of-range index rather than a zero-length attachment.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;
sz: Integer;
begin
// Demonstrates the GetAttachmentSize method, which returns the size in bytes of the
// attachment at the given zero-based index. It returns -1 if the index does not identify
// an existing attachment.
email := TEmail.Create;
email.Subject := 'Attachment size';
email.AddStringAttachment('notes.txt','Some notes for the attachment.');
// Get the size in bytes of the first attachment (index 0).
sz := email.GetAttachmentSize(0);
WriteLn('Attachment 0 size (bytes) = ' + sz);
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.