Sample code for 30+ languages & platforms
Unicode C

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 Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmailW email;

    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 = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Zip the attachments");

    CkEmailW_AddStringAttachment(email,L"a.txt",L"first attachment");
    CkEmailW_AddStringAttachment(email,L"b.txt",L"second attachment");
    wprintf(L"NumAttachments before = %d\n",CkEmailW_getNumAttachments(email));

    //  Replace all attachments with a single Zip attachment named "files.zip".
    success = CkEmailW_ZipAttachments(email,L"files.zip");
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        return true;
    }

    wprintf(L"NumAttachments after = %d\n",CkEmailW_getNumAttachments(email));


    CkEmailW_Dispose(email);

    }