Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Zip an Email's Attachments into One File
See more Email Object Examples
Demonstrates the Chilkat Email.ZipAttachments method, which replaces all of an email's attachments with a single Zip file attachment having the specified filename. The original attachments are removed and packed into the Zip. This example adds two attachments and zips them into one.
Background: Bundling multiple attachments into a single
.zip keeps a message tidy, compresses the payload, and works around recipients or gateways that limit the number of attachments. Note that the filename here (files.zip) names the attachment inside the message — it is not a path on disk. The reverse operation is UnzipAttachments.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
success: Boolean;
email: TEmail;
begin
success := False;
// Demonstrates the ZipAttachments method, which replaces all of an email's attachments
// with a single Zip file attachment having the specified filename. The original
// attachments are removed and packed into the Zip.
email := TEmail.Create;
email.Subject := 'Zip the attachments';
email.AddStringAttachment('a.txt','first attachment');
email.AddStringAttachment('b.txt','second attachment');
WriteLn('NumAttachments before = ' + email.NumAttachments);
// Replace all attachments with a single Zip attachment named "files.zip".
success := email.ZipAttachments('files.zip');
if (success = False) then
begin
WriteLn(email.LastErrorText);
Exit;
end;
WriteLn('NumAttachments after = ' + 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.