Sample code for 30+ languages & platforms
Unicode C

Remove a Single Attachment from an Email

See more Email Object Examples

Demonstrates the Chilkat Email.DropSingleAttachment method, which removes the attachment at the specified zero-based index. Valid indexes run from 0 through NumAttachments - 1. This example adds two attachments, removes the first, and prints the count before and after.

Background: Attachments are addressed by a zero-based index, so the first is 0, the second 1, and so on. DropSingleAttachment removes just one — use it to prune a specific file (say, an oversized or unwanted attachment) while keeping the rest. Note that after a removal the remaining attachments shift down, so if you are deleting several by index, iterate from the highest index downward to avoid skipping any.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkEmailW.h>

void ChilkatSample(void)
    {
    HCkEmailW email;

    //  Demonstrates the DropSingleAttachment method, which removes the attachment at the
    //  specified zero-based index.

    email = CkEmailW_Create();
    CkEmailW_putSubject(email,L"Drop a single attachment");

    CkEmailW_AddStringAttachment(email,L"a.txt",L"first attachment");
    CkEmailW_AddStringAttachment(email,L"b.txt",L"second attachment");
    wprintf(L"NumAttachments before = %d\n",CkEmailW_getNumAttachments(email));

    //  Remove the attachment at index 0 (the first attachment).
    CkEmailW_DropSingleAttachment(email,0);

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


    CkEmailW_Dispose(email);

    }