Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

C Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(C) Read IMAP Email Headers

This example demonstrates how to connect to an IMAP server and download only the email headers for all messages in a mailbox. Downloading headers only is much faster and more efficient than downloading complete emails because the message bodies and attachment contents are not retrieved.

The example shows how to:

  • Connect to an IMAP server using SSL/TLS
  • Authenticate and select a mailbox
  • Query for all messages in the mailbox
  • Fetch header-only email information
  • Display sender, subject, recipients, and total message size
  • Retrieve attachment metadata such as filenames and sizes without downloading the actual attachments

This technique is useful for quickly scanning large mailboxes, building message summaries, or inspecting attachment information while minimizing bandwidth and memory usage.

Note: This example requires Chilkat v11.0.0 or greater.

Chilkat C/C++ Library Downloads

MS Visual C/C++

C++ Builder

Linux C/C++

Alpine Linux C/C++

MacOS C/C++

iOS C/C++

Android C/C++

MinGW C/C++

#include <C_CkImap.h>
#include <C_CkMessageSet.h>
#include <C_CkEmailBundle.h>
#include <C_CkEmail.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkImap imap;
    BOOL fetchUids;
    HCkMessageSet messageSet;
    HCkEmailBundle bundle;
    BOOL headersOnly;
    HCkEmail email;
    const char *name;
    const char *addr;
    int i;
    int j;
    int numAttach;
    int attachSize;

    success = FALSE;

    success = FALSE;

    // This example assumes the Chilkat API has already been unlocked.
    // See Global Unlock Sample for example code.

    imap = CkImap_Create();

    // Connect to the IMAP server using SSL/TLS on the standard secure IMAP port.
    CkImap_putSsl(imap,TRUE);
    CkImap_putPort(imap,993);
    success = CkImap_Connect(imap,"imap.example.com");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    // Authenticate with the IMAP server.
    success = CkImap_Login(imap,"****","****");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    // Select the mailbox (folder) to access.
    success = CkImap_SelectMailbox(imap,"Inbox");
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        return;
    }

    // Get the identifiers for all messages in the selected mailbox.
    // Setting fetchUids = cktrue causes IMAP UIDs to be returned.
    // If ckfalse, IMAP sequence numbers are returned instead.
    fetchUids = TRUE;
    messageSet = CkMessageSet_Create();
    success = CkImap_QueryMbx(imap,"ALL",fetchUids,messageSet);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkMessageSet_Dispose(messageSet);
        return;
    }

    // Download only the email headers for the messages in the set.
    // This is much faster than downloading full emails because the
    // message bodies and attachment contents are not retrieved.
    // 
    // Even though the attachments themselves are not downloaded,
    // Chilkat can still provide information such as attachment
    // filenames, attachment sizes, and the total message size.
    bundle = CkEmailBundle_Create();
    headersOnly = TRUE;
    success = CkImap_FetchMsgSet(imap,headersOnly,messageSet,bundle);
    if (success == FALSE) {
        printf("%s\n",CkImap_lastErrorText(imap));
        CkImap_Dispose(imap);
        CkMessageSet_Dispose(messageSet);
        CkEmailBundle_Dispose(bundle);
        return;
    }

    // Iterate over each email in the bundle and display summary information.
    email = CkEmail_Create();

    i = 0;
    j = 0;
    while (i < CkEmailBundle_getMessageCount(bundle)) {
        CkEmailBundle_EmailAt(bundle,i,email);

        // Display the sender and subject line.
        printf("%s\n",CkEmail_ck_from(email));
        printf("%s\n",CkEmail_subject(email));

        // Display all "To" recipients.
        j = 0;
        while (j < CkEmail_getNumTo(email)) {
            printf("TO: %s, %s\n",CkEmail_getToName(email,j),CkEmail_getToAddr(email,j));
            j = j + 1;
        }

        // Display all "CC" recipients.
        j = 0;
        while (j < CkEmail_getNumCC(email)) {
            printf("CC: %s, %s\n",CkEmail_getCcName(email,j),CkEmail_getCcAddr(email,j));
            j = j + 1;
        }

        // Display the total size of the email message, including
        // the body and all attachments, even though only headers
        // were downloaded.
        printf("%d\n",CkEmail_getSize(email));

        // When full emails are downloaded, attachment information
        // is accessed using the email.NumAttachments property and
        // the email.GetMailAttach* methods.
        // 
        // However, because this example downloaded headers only,
        // attachment metadata must be obtained using the IMAP object.
        numAttach = CkImap_GetMailNumAttach(imap,email);

        j = 0;
        while (j < numAttach) {

            // Display the attachment filename.
            printf("%s\n",CkImap_getMailAttachFilename(imap,email,j));

            // Display the attachment size in bytes.
            attachSize = CkImap_GetMailAttachSize(imap,email,j);
            printf("    size = %d bytes\n",attachSize);

            j = j + 1;
        }

        printf("--\n");

        i = i + 1;
    }

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


    CkImap_Dispose(imap);
    CkMessageSet_Dispose(messageSet);
    CkEmailBundle_Dispose(bundle);
    CkEmail_Dispose(email);

    }

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.