Sample code for 30+ languages & platforms
Unicode C

Add an Attachment from a BinData Object

See more Email Object Examples

Demonstrates the Chilkat Email.AddAttachmentBd method, which adds an attachment using the contents of a BinData object. The first argument is the attachment filename, the second is the BinData, and the third is the content type — if empty, it is inferred from the filename extension. This example loads a PDF into a BinData and attaches it.

Background: BinData is Chilkat's container for raw binary data. Attaching from a BinData is the right approach when the file's bytes are already in memory — generated on the fly, downloaded, or read from a database — rather than sitting on disk (which would use AddFileAttachment). Chilkat Base64-encodes the bytes into the message automatically.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>
#include <C_CkBinDataW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkEmailW email;
    HCkBinDataW bd;

    success = FALSE;

    //  Demonstrates the AddAttachmentBd method, which adds an attachment using the contents of a
    //  BinData object.  The first argument is the attachment filename, the second is the BinData
    //  object, and the third is the content type (inferred from the filename extension if empty).

    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Attach from BinData");
    CkEmailW_putBody(email,L"Please see the attached file.");

    //  Load a file into a BinData object, then attach it.
    bd = CkBinDataW_Create();
    success = CkBinDataW_LoadFile(bd,L"qa_data/attachments/report.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkBinDataW_lastErrorText(bd));
        CkEmailW_Dispose(email);
        CkBinDataW_Dispose(bd);
        return true;
    }

    success = CkEmailW_AddAttachmentBd(email,L"report.pdf",bd,L"application/pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkEmailW_lastErrorText(email));
        CkEmailW_Dispose(email);
        CkBinDataW_Dispose(bd);
        return true;
    }

    wprintf(L"NumAttachments = %d\n",CkEmailW_getNumAttachments(email));

    //  Note: The path "qa_data/attachments/report.pdf" is a relative local filesystem path,
    //  relative to the current working directory of the running application.


    CkEmailW_Dispose(email);
    CkBinDataW_Dispose(bd);

    }