Sample code for 30+ languages & platforms
Unicode C

Attach a File to a Multipart MIME Entity

See more MIME Examples

Demonstrates the Chilkat Mime.AppendPartFromFile method, which reads a local file, creates an attachment part, and appends it. The only argument is the file path; the content headers are chosen from the filename extension.

Note: The file paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the one-call way to add an attachment: Chilkat reads the file, infers its Content-Type from the extension, base64-encodes binary content, and appends it as a child part — typically inside a multipart/mixed. It is the building block behind adding attachments to an email, saving you from constructing the part and setting its headers by hand.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkMimeW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkMimeW mime;
    HCkMimeW bodyPart;

    success = FALSE;

    //  Demonstrates the Mime.AppendPartFromFile method, which reads a local file, creates an attachment
    //  part, and appends it.  The only argument is the file path; the content headers are chosen from
    //  the filename extension.

    mime = CkMimeW_Create();
    success = CkMimeW_NewMultipartMixed(mime);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        return;
    }

    //  A message body part.
    bodyPart = CkMimeW_Create();
    success = CkMimeW_SetBodyFromPlainText(bodyPart,L"Please see the attached files.");
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(bodyPart));
        CkMimeW_Dispose(mime);
        CkMimeW_Dispose(bodyPart);
        return;
    }

    success = CkMimeW_AppendPart(mime,bodyPart);
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        CkMimeW_Dispose(bodyPart);
        return;
    }

    //  Attach files directly.
    success = CkMimeW_AppendPartFromFile(mime,L"qa_data/report.pdf");
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        CkMimeW_Dispose(bodyPart);
        return;
    }

    success = CkMimeW_AppendPartFromFile(mime,L"qa_data/photo.jpg");
    if (success == FALSE) {
        wprintf(L"%s\n",CkMimeW_lastErrorText(mime));
        CkMimeW_Dispose(mime);
        CkMimeW_Dispose(bodyPart);
        return;
    }

    wprintf(L"Number of parts: %d\n",CkMimeW_getNumParts(mime));


    CkMimeW_Dispose(mime);
    CkMimeW_Dispose(bodyPart);

    }