Sample code for 30+ languages & platforms
Unicode C

Add a String Attachment to an Email

See more Email Object Examples

Demonstrates the Chilkat Email object's AddStringAttachment method, which adds an attachment directly from an in-memory string — no file needs to exist on disk. The 1st argument is the filename that is written into the MIME (it is not a path to a file that is read), and the 2nd argument is the text that becomes the attachment's body. In this example we build a simple email, attach a small CSV file named people.csv straight from a string, and then print the resulting MIME so you can see the attachment embedded in the message.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>

void ChilkatSample(void)
    {
    HCkEmailW email;
    const wchar_t *csvData;

    //  This example demonstrates the Email object's AddStringAttachment method.
    //  AddStringAttachment adds an attachment directly from an in-memory string.
    //  The 1st argument is the filename to be placed in the MIME (not a file to be read).
    //  The 2nd argument is the text content that becomes the attachment body.

    email = CkEmailW_Create();

    CkEmailW_putSubject(email,L"Email with a string attachment");
    CkEmailW_putBody(email,L"See the attached CSV file.");
    CkEmailW_putFrom(email,L"mary@example.com");
    CkEmailW_AddTo(email,L"Joe",L"joe@example.com");

    //  The text content of the attachment.
    csvData = L"id,name\r\n1,Alice\r\n2,Bob\r\n";

    //  Add the string as an attachment named "people.csv".
    CkEmailW_AddStringAttachment(email,L"people.csv",csvData);

    //  Show the full MIME of the email, which now includes the attachment.
    wprintf(L"%s\n",CkEmailW_getMime(email));


    CkEmailW_Dispose(email);

    }