Sample code for 30+ languages & platforms
Delphi ActiveX

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 Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
email: TChilkatEmail;

begin
success := 0;

//  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 := TChilkatEmail.Create(Self);
email.Subject := 'Zip the attachments';

email.AddStringAttachment('a.txt','first attachment');
email.AddStringAttachment('b.txt','second attachment');
Memo1.Lines.Add('NumAttachments before = ' + IntToStr(email.NumAttachments));

//  Replace all attachments with a single Zip attachment named "files.zip".
success := email.ZipAttachments('files.zip');
if (success = 0) then
  begin
    Memo1.Lines.Add(email.LastErrorText);
    Exit;
  end;

Memo1.Lines.Add('NumAttachments after = ' + IntToStr(email.NumAttachments));