Sample code for 30+ languages & platforms
C++

Get the Nth Binary Part of a Content-Type into BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetNthBinaryPartOfTypeBd method, which loads the binary bytes of the Nth MIME sub-part matching a content-type pattern into a BinData object. The arguments are the zero-based index among the matching parts, the content-type pattern, an inlineOnly flag, an excludeAttachments flag, and the BinData that receives the bytes. This example extracts the first image/png part.

Background: This is the binary, type-targeted way to pull a specific part out of a message — ideal for extracting, say, every image/png or the one application/pdf from a complex MIME tree without caring whether it is an attachment, an inline image, or a body part. Reading into a BinData keeps the raw bytes exact, ready to save, hash, or re-transmit.

Chilkat C++ Downloads

C++
#include <CkEmail.h>
#include <CkBinData.h>

void ChilkatSample(void)
    {
    bool success = false;

    //  Demonstrates the GetNthBinaryPartOfTypeBd method, which loads the binary bytes of the Nth
    //  MIME sub-part matching a Content-Type pattern into a BinData object.  The arguments are
    //  the zero-based index among matching parts, the Content-Type pattern, inlineOnly,
    //  excludeAttachments, and the BinData that receives the bytes.

    CkEmail email;
    email.put_Subject("GetNthBinaryPartOfTypeBd example");
    email.put_Body("See the attached image.");

    //  Load the image from a file into a BinData object and attach it (binary data belongs
    //  in a BinData, never in a string).
    CkBinData bdImage;
    success = bdImage.LoadFile("qa_data/images/photo.png");
    if (success == false) {
        std::cout << bdImage.lastErrorText() << "\r\n";
        return true;
    }

    success = email.AddAttachmentBd("photo.png",bdImage,"image/png");
    if (success == false) {
        std::cout << email.lastErrorText() << "\r\n";
        return true;
    }

    //  Load the bytes of the first (index 0) image/png part into a BinData object.
    CkBinData bd;
    success = email.GetNthBinaryPartOfTypeBd(0,"image/png",false,false,bd);
    if (success == false) {
        std::cout << email.lastErrorText() << "\r\n";
        return true;
    }

    std::cout << "image/png part size (bytes) = " << bd.get_NumBytes() << "\r\n";

    //  Note: The path "qa_data/images/photo.png" is a relative local filesystem path,
    //  relative to the current working directory of the running application.
    }