Sample code for 30+ languages & platforms
C

Add File Attachments to an Email

Demonstrates how to add one or more file attachments to an email.

Chilkat C Downloads

C
#include <C_CkEmail.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail email;
    const char *contentType;

    success = FALSE;

    email = CkEmail_Create();

    CkEmail_putSubject(email,"This is a test");
    CkEmail_putBody(email,"This is a test");
    CkEmail_putFrom(email,"support@chilkatsoft.com");
    success = CkEmail_AddTo(email,"Chilkat Admin","admin@chilkatsoft.com");

    // To add file attachments to an email, call AddFileAttachment
    // once for each file to be attached.  The method returns
    // the content-type of the attachment if successful, otherwise
    // returns cknull

    contentType = CkEmail_addFileAttachment(email,"something.pdf");
    if (CkEmail_getLastMethodSuccess(email) != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    contentType = CkEmail_addFileAttachment(email,"something.xml");
    if (CkEmail_getLastMethodSuccess(email) != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    contentType = CkEmail_addFileAttachment(email,"something.zip");
    if (CkEmail_getLastMethodSuccess(email) != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    success = CkEmail_SaveEml(email,"email.eml");
    if (success == FALSE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    printf("Saved EML!\n");


    CkEmail_Dispose(email);

    }