![]() |
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
(Unicode C) Read IMAP Email HeadersCall FetchHeaders to download only the email headers. 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; // This example requires 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. // 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 = 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; while (i < CkEmailBundleW_getMessageCount(bundle)) { 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"); } // Disconnect from the IMAP server. success = CkImapW_Disconnect(imap); CkImapW_Dispose(imap); CkMessageSetW_Dispose(messageSet); CkEmailBundleW_Dispose(bundle); CkEmailW_Dispose(email); } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.