![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java 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
(C) Read IMAP Email HeadersCall FetchHeaders to download only the email headers. Note: This example requires Chilkat v11.0.0 or greater.
#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; // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. imap = CkImap_Create(); // Connect to an IMAP server. // Use TLS 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; } // Login success = CkImap_Login(imap,"****","****"); if (success == FALSE) { printf("%s\n",CkImap_lastErrorText(imap)); CkImap_Dispose(imap); return; } // Select an IMAP mailbox success = CkImap_SelectMailbox(imap,"Inbox"); if (success == FALSE) { printf("%s\n",CkImap_lastErrorText(imap)); CkImap_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 = 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; } // When downloading headers, each email object contains // (obviously) the headers, but the body will be missing. // Also, 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 = 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; } // Loop over the email objects and display information // about each: email = CkEmail_Create(); i = 0; j = 0; while (i < CkEmailBundle_getMessageCount(bundle)) { CkEmailBundle_EmailAt(bundle,i,email); // Display the From and Subject printf("%s\n",CkEmail_ck_from(email)); printf("%s\n",CkEmail_subject(email)); // Display the recipients: j = 0; while (j < CkEmail_getNumTo(email)) { printf("TO: %s, %s\n",CkEmail_getToName(email,j),CkEmail_getToAddr(email,j)); j = j + 1; } j = 0; while (j < CkEmail_getNumCC(email)) { printf("CC: %s, %s\n",CkEmail_getCcName(email,j),CkEmail_getCcAddr(email,j)); j = j + 1; } // Show the total size of the email, including body and attachments: printf("%d\n",CkEmail_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 = CkImap_GetMailNumAttach(imap,email); j = 0; while (j < numAttach) { printf("%s\n",CkImap_getMailAttachFilename(imap,email,j)); attachSize = CkImap_GetMailAttachSize(imap,email,j); printf(" size = %d bytes\n",attachSize); j = j + 1; } printf("--\n"); } // Disconnect from the IMAP server. success = CkImap_Disconnect(imap); CkImap_Dispose(imap); CkMessageSet_Dispose(messageSet); CkEmailBundle_Dispose(bundle); CkEmail_Dispose(email); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.