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

Find and save an attachment and list inline images

See more Email Object Examples

Find an attachment by filename and media type, then save its decoded bytes to a path you choose. Enumerate inline resources separately to read their Content-IDs.

Chilkat C++ Downloads

C++
#include <CkEmail.h>
#include <CkStringBuilder.h>
#include <CkBinData.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;
    }

    CkStringBuilder filename;
    CkStringBuilder mediaType;
    bool found = false;
    int i = 0;
    while ((i < email.get_NumAttachments())) {
        filename.SetString(email.getAttachmentFilename(i));
        mediaType.SetString(email.getAttachmentContentType(i));
        if ((filename.ContentsEqual("agenda.csv",false) == true) && (mediaType.ContentsEqual("text/csv",false) == true) && (found == false)) {
            CkBinData data;
            success = email.GetAttachmentBd(i,data);
            if (success != true) {
                std::cout << email.lastErrorText() << "\r\n";
                return;
            }

            success = data.WriteFile("received-agenda.csv");
            if (success != true) {
                std::cout << "Failed: data.WriteFile." << "\r\n";
                return;
            }

            std::cout << "Saved attachment: " << data.get_NumBytes() << " bytes." << "\r\n";
            found = true;
        }

        i = i + 1;
    }

    if (found != true) {
        std::cout << "No matching attachment." << "\r\n";
    }

    i = 0;
    while ((i < email.get_NumRelatedItems())) {
        std::cout << "Related: " << email.getRelatedContentType(i) << "  Content-ID: " << email.getRelatedContentID(i) << "\r\n";
        i = i + 1;
    }
    }