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

Create an HTML preview with embedded images

See more Email Object Examples

Make an independent copy of with-attachments.eml, remove ordinary file attachments from the copy, and export its HTML with related images as data URLs. The original retains its attachments. Use a sanitized viewer when showing untrusted HTML.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail email;
    HCkEmail preview;
    HCkStringBuilder html;

    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;
    }

    preview = CkEmail_Create();
    success = CkEmail_MakeCopy(email,preview);
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        CkEmail_Dispose(preview);
        return;
    }

    //  Keep related items: the HTML still refers to them.
    CkEmail_DropAttachments(preview);
    printf("Files in original: %d  Files in preview: %d\n",CkEmail_getNumAttachments(email),CkEmail_getNumAttachments(preview));
    if (CkEmail_HasHtmlBody(preview) != TRUE) {
        printf("This message has no HTML preview.\n");
        CkEmail_Dispose(email);
        CkEmail_Dispose(preview);
        return;
    }

    html = CkStringBuilder_Create();
    success = CkEmail_GetHtmlBodySb(preview,TRUE,html);
    if (success != TRUE) {
        printf("%s\n",CkEmail_lastErrorText(preview));
        CkEmail_Dispose(email);
        CkEmail_Dispose(preview);
        CkStringBuilder_Dispose(html);
        return;
    }

    success = CkStringBuilder_WriteFile(html,"preview.html","utf-8",FALSE);
    if (success != TRUE) {
        printf("Failed: html.WriteFile.\n");
        CkEmail_Dispose(email);
        CkEmail_Dispose(preview);
        CkStringBuilder_Dispose(html);
        return;
    }

    printf("Saved preview.html.\n");


    CkEmail_Dispose(email);
    CkEmail_Dispose(preview);
    CkStringBuilder_Dispose(html);

    }