Sample code for 30+ languages & platforms
Unicode C

Imap.GetMailSize vs Email.Size

Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkImapW.h>
#include <C_CkMessageSetW.h>
#include <C_CkEmailBundleW.h>
#include <C_CkEmailW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImapW imap;
    BOOL fetchUids;
    HCkMessageSetW messageSet;
    HCkEmailBundleW bundle;
    BOOL headersOnly;
    HCkEmailW email;
    int i;
    int j;
    int numEmails;
    int numAttach;
    int attachSize;

    success = FALSE;

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    imap = CkImapW_Create();

    // Connect to an IMAP server.
    // Use TLS
    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;
    }

    // Login
    success = CkImapW_Login(imap,L"****",L"****");
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        return;
    }

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

    // Get the message IDs of all the emails in the mailbox
    // We can choose to fetch UIDs or sequence numbers.
    fetchUids = TRUE;
    messageSet = CkMessageSetW_Create();
    success = CkImapW_QueryMbx(imap,L"ALL",fetchUids,messageSet);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messageSet);
        return;
    }

    // When downloading headers, each email object contains
    // (obviously) the headers, but the body will be missing.
    // Attachments will not be included.  However, it is
    // possible to get information about the attachments
    // as well as the complete size of the email.
    bundle = CkEmailBundleW_Create();
    headersOnly = TRUE;
    success = CkImapW_FetchMsgSet(imap,headersOnly,messageSet,bundle);
    if (success == FALSE) {
        wprintf(L"%s\n",CkImapW_lastErrorText(imap));
        CkImapW_Dispose(imap);
        CkMessageSetW_Dispose(messageSet);
        CkEmailBundleW_Dispose(bundle);
        return;
    }

    // Loop over the email objects and display information about each:
    email = CkEmailW_Create();
    i = 0;
    j = 0;
    numEmails = CkEmailBundleW_getMessageCount(bundle);
    while (i < numEmails) {
        CkEmailBundleW_EmailAt(bundle,i,email);

        // Display the From and Subject
        wprintf(L"%s\n",CkEmailW_ck_from(email));
        wprintf(L"%s\n",CkEmailW_subject(email));

        // Display the recipients:
        j = 0;
        while (j < CkEmailW_getNumTo(email)) {
            wprintf(L"TO: %s, %s\n",CkEmailW_getToName(email,j),CkEmailW_getToAddr(email,j));
            j = j + 1;
        }

        j = 0;
        while (j < CkEmailW_getNumCC(email)) {
            wprintf(L"CC: %s, %s\n",CkEmailW_getCcName(email,j),CkEmailW_getCcAddr(email,j));
            j = j + 1;
        }

        // Show the total size of the email, including body and attachments:
        wprintf(L"%d\n",CkEmailW_getSize(email));

        // When a full email is downloaded, we would use the
        // email.NumAttachments property in conjunction with the
        // email.GetMailAttach* methods.
        // However, when an email object contains only the header,
        // we need to access the attachment info differently:
        numAttach = CkImapW_GetMailNumAttach(imap,email);
        j = 0;
        while (j < numAttach) {
            wprintf(L"%s\n",CkImapW_getMailAttachFilename(imap,email,j));
            attachSize = CkImapW_GetMailAttachSize(imap,email,j);
            wprintf(L"    size = %d bytes\n",attachSize);
            j = j + 1;
        }

        wprintf(L"--\n");

        i = i + 1;
    }

    // Disconnect from the IMAP server.
    success = CkImapW_Disconnect(imap);


    CkImapW_Dispose(imap);
    CkMessageSetW_Dispose(messageSet);
    CkEmailBundleW_Dispose(bundle);
    CkEmailW_Dispose(email);

    }