Sample code for 30+ languages & platforms
Unicode C

Save All Email Attachments to a Directory

See more Email Object Examples

Demonstrates the Chilkat Email.SaveAllAttachments method, which saves all of the email's attachments to a directory. If the directory or any of its components do not exist, Chilkat creates them automatically. This example adds two attachments and saves them.

Background: Each attachment is written using its own filename. Because those filenames come from the sender and may collide, consider the OverwriteExisting property (which can auto-generate unique names) and RemoveAttachmentPaths (which strips embedded path info) to keep saved files safe and non-clobbering — important when processing untrusted incoming mail.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>

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

    success = FALSE;

    //  Demonstrates the SaveAllAttachments method, which saves all attachments to a directory.
    //  If the directory (or any of its components) does not exist, Chilkat creates it.

    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Save all attachments");

    CkEmailW_AddStringAttachment(email,L"a.txt",L"first attachment");
    CkEmailW_AddStringAttachment(email,L"b.txt",L"second attachment");

    //  Save every attachment into the specified directory.
    success = CkEmailW_SaveAllAttachments(email,L"qa_output/attachments");
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        return true;
    }

    wprintf(L"Saved all attachments.\n");

    //  Note: The path "qa_output/..." is a relative local filesystem path,
    //  relative to the current working directory of the running application.


    CkEmailW_Dispose(email);

    }