![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(Unicode C) Read IMAP Email HeadersThis 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:
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.
#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; const wchar_t *name; const wchar_t *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 = CkImapW_Create(); // Connect to the IMAP server using SSL/TLS on the standard secure IMAP port. 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; } // Authenticate with the IMAP server. success = CkImapW_Login(imap,L"****",L"****"); if (success == FALSE) { wprintf(L"%s\n",CkImapW_lastErrorText(imap)); CkImapW_Dispose(imap); return; } // Select the mailbox (folder) to access. success = CkImapW_SelectMailbox(imap,L"Inbox"); if (success == FALSE) { wprintf(L"%s\n",CkImapW_lastErrorText(imap)); CkImapW_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 = 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; } // 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 = 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; } // Iterate over each email in the bundle and display summary information. email = CkEmailW_Create(); i = 0; j = 0; while (i < CkEmailBundleW_getMessageCount(bundle)) { CkEmailBundleW_EmailAt(bundle,i,email); // Display the sender and subject line. wprintf(L"%s\n",CkEmailW_ck_from(email)); wprintf(L"%s\n",CkEmailW_subject(email)); // Display all "To" 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; } // Display all "CC" recipients. j = 0; while (j < CkEmailW_getNumCC(email)) { wprintf(L"CC: %s, %s\n",CkEmailW_getCcName(email,j),CkEmailW_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. wprintf(L"%d\n",CkEmailW_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 = CkImapW_GetMailNumAttach(imap,email); j = 0; while (j < numAttach) { // Display the attachment filename. wprintf(L"%s\n",CkImapW_getMailAttachFilename(imap,email,j)); // Display the attachment size in bytes. 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); } |
||||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.