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

Read email headers and display the body as text

See more Email Object Examples

Read the sender, recipients, subject, date, and an optional custom header from an EML file. Display the plain-text body when available, or convert HTML to text. Handle messages that contain neither body format.

Chilkat C Downloads

C
#include <C_CkEmail.h>
#include <C_CkHtmlToText.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail email;
    int i;
    const char *referenceId;
    HCkHtmlToText reader;

    success = FALSE;

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

    email = CkEmail_Create();
    success = CkEmail_LoadEml(email,"with-attachments.eml");
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        return;
    }

    printf("From: %s\n",CkEmail_ck_from(email));
    printf("Subject: %s\n",CkEmail_subject(email));
    printf("Date: %s\n",CkEmail_emailDateStr(email));
    i = 0;
    while ((i < CkEmail_getNumTo(email))) {
        printf("To: %s  %s\n",CkEmail_getToName(email,i),CkEmail_getToAddr(email,i));
        i = i + 1;
    }

    referenceId = CkEmail_getHeaderField(email,"X-Reference-ID");
    if (CkEmail_getLastMethodSuccess(email) == TRUE) {
        printf("Reference: %s\n",referenceId);
    }

    if (CkEmail_HasPlainTextBody(email) == TRUE) {
        printf("%s\n",CkEmail_getPlainTextBody(email));
    }
    else {
        if (CkEmail_HasHtmlBody(email) == TRUE) {
            reader = CkHtmlToText_Create();
            printf("%s\n",CkHtmlToText_toText(reader,CkEmail_getHtmlBody(email)));
        }
        else {
            printf("No text or HTML body.\n");
        }

    }



    CkEmail_Dispose(email);
    CkHtmlToText_Dispose(reader);

    }