![]() |
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
(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.
bool success = false; success = false; // This example assumes the Chilkat API has already been unlocked. // See Global Unlock Sample for example code. Chilkat.Imap imap = new Chilkat.Imap(); // Connect to the IMAP server using SSL/TLS on the standard secure IMAP port. imap.Ssl = true; imap.Port = 993; success = imap.Connect("imap.example.com"); if (success == false) { Debug.WriteLine(imap.LastErrorText); return; } // Authenticate with the IMAP server. success = imap.Login("****","****"); if (success == false) { Debug.WriteLine(imap.LastErrorText); return; } // Select the mailbox (folder) to access. success = imap.SelectMailbox("Inbox"); if (success == false) { Debug.WriteLine(imap.LastErrorText); 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. bool fetchUids = true; Chilkat.MessageSet messageSet = new Chilkat.MessageSet(); success = imap.QueryMbx("ALL",fetchUids,messageSet); if (success == false) { Debug.WriteLine(imap.LastErrorText); 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. Chilkat.EmailBundle bundle = new Chilkat.EmailBundle(); bool headersOnly = true; success = imap.FetchMsgSet(headersOnly,messageSet,bundle); if (success == false) { Debug.WriteLine(imap.LastErrorText); return; } // Iterate over each email in the bundle and display summary information. Chilkat.Email email = new Chilkat.Email(); string name; string addr; int i = 0; int j = 0; while (i < bundle.MessageCount) { bundle.EmailAt(i,email); // Display the sender and subject line. Debug.WriteLine(email.From); Debug.WriteLine(email.Subject); // Display all "To" recipients. j = 0; while (j < email.NumTo) { Debug.WriteLine("TO: " + email.GetToName(j) + ", " + email.GetToAddr(j)); j = j + 1; } // Display all "CC" recipients. j = 0; while (j < email.NumCC) { Debug.WriteLine("CC: " + email.GetCcName(j) + ", " + email.GetCcAddr(j)); j = j + 1; } // Display the total size of the email message, including // the body and all attachments, even though only headers // were downloaded. Debug.WriteLine(Convert.ToString(email.Size)); // 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. int numAttach = imap.GetMailNumAttach(email); j = 0; while (j < numAttach) { // Display the attachment filename. Debug.WriteLine(imap.GetMailAttachFilename(email,j)); // Display the attachment size in bytes. int attachSize = imap.GetMailAttachSize(email,j); Debug.WriteLine(" size = " + Convert.ToString(attachSize) + " bytes"); j = j + 1; } Debug.WriteLine("--"); i = i + 1; } // Disconnect from the IMAP server. success = imap.Disconnect(); |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.