Sample code for 30+ languages & platforms
C

Get an Attached Message as an Email Object

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachedEmail method, which copies the Nth embedded message/rfc822 MIME part into another Email object. The index is zero-based (valid values run from 0 through NumAttachedMessages - 1). This example attaches an email and then extracts it back into its own object.

Background: When a message forwards another email as an attachment, that nested message is a complete email in its own right. GetAttachedEmail turns it back into a fully navigable Email object so you can read its subject, sender, body, and even its own attachments — essential for processing forwarded mail, or for tools that unpack reported spam/phishing to examine the original.

Chilkat C Downloads

C
#include <C_CkEmail.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail innerEmail;
    HCkEmail email;
    HCkEmail attached;

    success = FALSE;

    //  Demonstrates the GetAttachedEmail method, which copies the Nth embedded message/rfc822
    //  MIME part into another Email object.  The index is zero-based.

    //  Build an inner email and attach it to an outer email.
    innerEmail = CkEmail_Create();
    CkEmail_putSubject(innerEmail,"Embedded message");
    CkEmail_putFrom(innerEmail,"alice@example.com");
    CkEmail_putBody(innerEmail,"This is the embedded message.");

    email = CkEmail_Create();
    CkEmail_putSubject(email,"Has an attached message");
    success = CkEmail_AttachEmail(email,innerEmail);
    if (success == FALSE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(innerEmail);
        CkEmail_Dispose(email);
        return;
    }

    //  Copy the first embedded message (index 0) into its own Email object.
    attached = CkEmail_Create();
    success = CkEmail_GetAttachedEmail(email,0,attached);
    if (success == FALSE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(innerEmail);
        CkEmail_Dispose(email);
        CkEmail_Dispose(attached);
        return;
    }

    printf("Attached email subject: %s\n",CkEmail_subject(attached));
    printf("Attached email from: %s\n",CkEmail_ck_from(attached));


    CkEmail_Dispose(innerEmail);
    CkEmail_Dispose(email);
    CkEmail_Dispose(attached);

    }