Sample code for 30+ languages & platforms
C Requires Chilkat v11.0.0+

Read an attached email

See more Email Object Examples

Open forward-as-attachment.eml from the forwarding example. Read the cover note’s subject, then open each attached message and inspect its own headers and attachment count. This works even when the outer message has no ordinary files.

Chilkat C Downloads

C
#include <C_CkEmail.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail wrapper;
    HCkEmail nested;
    int i;

    success = FALSE;

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    wrapper = CkEmail_Create();
    success = CkEmail_LoadEml(wrapper,"forward-as-attachment.eml");
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(wrapper));
        CkEmail_Dispose(wrapper);
        return;
    }

    printf("Cover note: %s\n",CkEmail_subject(wrapper));
    if (CkEmail_getNumAttachedMessages(wrapper) == 0) {
        printf("No attached messages.\n");
        CkEmail_Dispose(wrapper);
        return;
    }

    nested = CkEmail_Create();
    i = 0;
    while ((i < CkEmail_getNumAttachedMessages(wrapper))) {
        success = CkEmail_GetAttachedEmail(wrapper,i,nested);
        if (success != TRUE) {
            printf("%s\n",CkEmail_lastErrorText(wrapper));
            CkEmail_Dispose(wrapper);
            CkEmail_Dispose(nested);
            return;
        }

        printf("Original author: %s\n",CkEmail_ck_from(nested));
        printf("Original subject: %s\n",CkEmail_subject(nested));
        printf("Files on the original: %d\n",CkEmail_getNumAttachments(nested));
        i = i + 1;
    }



    CkEmail_Dispose(wrapper);
    CkEmail_Dispose(nested);

    }