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 <CkEmail.h>
#include <CkStringBuilder.h>

void ChilkatSample(void)
    {
    bool success = false;

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

    CkEmail email;
    success = email.LoadEml("with-attachments.eml");
    if (success != true) {
        std::cout << email.lastErrorText() << "\r\n";
        return;
    }

    CkEmail preview;
    success = email.MakeCopy(preview);
    if (success != true) {
        std::cout << email.lastErrorText() << "\r\n";
        return;
    }

    //  Keep related items: the HTML still refers to them.
    preview.DropAttachments();
    std::cout << "Files in original: " << email.get_NumAttachments() << "  Files in preview: " << preview.get_NumAttachments() << "\r\n";
    if (preview.HasHtmlBody() != true) {
        std::cout << "This message has no HTML preview." << "\r\n";
        return;
    }

    CkStringBuilder html;
    success = preview.GetHtmlBodySb(true,html);
    if (success != true) {
        std::cout << preview.lastErrorText() << "\r\n";
        return;
    }

    success = html.WriteFile("preview.html","utf-8",false);
    if (success != true) {
        std::cout << "Failed: html.WriteFile." << "\r\n";
        return;
    }

    std::cout << "Saved preview.html." << "\r\n";
    }