Sample code for 30+ languages & platforms
C

Get an Attachment's Bytes into a BinData

See more Email Object Examples

Demonstrates the Chilkat Email.GetAttachmentBd method, which copies an attachment's binary data into a BinData object. The first attachment is at index 0. This example adds an attachment and copies its bytes into a BinData, printing the byte count.

Background: This is the safe, binary way to extract an attachment — the counterpart to the text-oriented GetAttachmentString. Because attachments are often binary (PDFs, images, archives), copying the raw bytes into a BinData preserves them exactly, ready to write to a file, hash, or pass to another API without any charset conversion that could corrupt the data.

Chilkat C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmail email;
    HCkBinData bd;

    success = FALSE;

    //  Demonstrates the GetAttachmentBd method, which copies an attachment's binary data into a
    //  BinData object.  The first attachment is at index 0.

    email = CkEmail_Create();
    CkEmail_putSubject(email,"GetAttachmentBd example");

    CkEmail_AddStringAttachment(email,"notes.txt","Some notes stored in the attachment.");

    //  Copy the first attachment's binary data into a BinData object.
    bd = CkBinData_Create();
    success = CkEmail_GetAttachmentBd(email,0,bd);
    if (success == FALSE) {
        printf("%s\n",CkEmail_lastErrorText(email));
        CkEmail_Dispose(email);
        CkBinData_Dispose(bd);
        return;
    }

    printf("Attachment size (bytes) = %d\n",CkBinData_getNumBytes(bd));


    CkEmail_Dispose(email);
    CkBinData_Dispose(bd);

    }