Sample code for 30+ languages & platforms
Unicode C

Fetch an IMAP Attachment into a BinData

See more IMAP Examples

Demonstrates the Chilkat Imap.FetchAttachmentBd method, which stores one attachment's bytes in a BinData object. The first argument is the Email, the second is the zero-based attachment index, and the third is the BinData, which is cleared before the operation. This example fetches the first attachment and prints its byte count.

Background: A BinData holds raw bytes, so it works for any attachment type — image, PDF, zip, and so on. From there you can save it, hash it, or hand it to another API expecting a byte buffer, all without touching the filesystem. Use the string/StringBuilder variants only when the attachment is known to be text.

Chilkat Unicode C Downloads

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

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    BOOL headersOnly;
    BOOL useUid;
    int seqNum;
    HCkEmailW email;
    int numAttach;
    HCkBinDataW bd;

    success = FALSE;

    //  Demonstrates the Imap.FetchAttachmentBd method, which downloads one attachment's bytes into
    //  a BinData object.  The 1st argument is the Email, the 2nd is the zero-based attachment
    //  index, and the 3rd is the BinData (which is cleared first).
    //  
    //  The message is fetched headers-only, then the attachment is downloaded on demand.

    imap = CkImapW_Create();

    CkImapW_putSsl(imap,TRUE);
    CkImapW_putPort(imap,993);

    success = CkImapW_Connect(imap,L"imap.example.com");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    success = CkImapW_Login(imap,L"user@example.com",L"myPassword");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    success = CkImapW_SelectMailbox(imap,L"Inbox");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

    //  Fetch only the message headers.  Attachment bodies are NOT downloaded, but the ckx-imap-*
    //  metadata describing the attachments is present, so the attachment info methods still work.
    headersOnly = TRUE;
    useUid = FALSE;
    seqNum = 1;
    email = CkEmailW_Create();
    success = CkImapW_FetchEmail(imap,headersOnly,seqNum,useUid,email);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkEmailW_Dispose(email);
        return;
    }

    numAttach = CkImapW_GetMailNumAttach(imap,email);
    if (numAttach > 0) {
        //  Download just the first attachment's bytes from the server into a BinData.
        bd = CkBinDataW_Create();
        success = CkImapW_FetchAttachmentBd(imap,email,0,bd);
        if (success == FALSE) {
            wprintf(L"%s\n",CkImapW_lastErrorText(imap));
            CkImapW_Dispose(imap);
            CkEmailW_Dispose(email);
            CkBinDataW_Dispose(bd);
            return;
        }

        wprintf(L"Attachment size: %d bytes\n",CkBinDataW_getNumBytes(bd));
    }

    success = CkImapW_Disconnect(imap);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkEmailW_Dispose(email);
        CkBinDataW_Dispose(bd);
        return;
    }



    CkImapW_Dispose(imap);
    CkEmailW_Dispose(email);
    CkBinDataW_Dispose(bd);

    }