![]() |
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
(Chilkat2-Python) 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.
import sys import chilkat2 success = False success = False # This example assumes the Chilkat API has already been unlocked. # See Global Unlock Sample for example code. imap = chilkat2.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): print(imap.LastErrorText) sys.exit() # Authenticate with the IMAP server. success = imap.Login("****","****") if (success == False): print(imap.LastErrorText) sys.exit() # Select the mailbox (folder) to access. success = imap.SelectMailbox("Inbox") if (success == False): print(imap.LastErrorText) sys.exit() # 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 = chilkat2.MessageSet() success = imap.QueryMbx("ALL",fetchUids,messageSet) if (success == False): print(imap.LastErrorText) sys.exit() # 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 = chilkat2.EmailBundle() headersOnly = True success = imap.FetchMsgSet(headersOnly,messageSet,bundle) if (success == False): print(imap.LastErrorText) sys.exit() # Iterate over each email in the bundle and display summary information. email = chilkat2.Email() i = 0 j = 0 while i < bundle.MessageCount : bundle.EmailAt(i,email) # Display the sender and subject line. print(email.From) print(email.Subject) # Display all "To" recipients. j = 0 while j < email.NumTo : print("TO: " + email.GetToName(j) + ", " + email.GetToAddr(j)) j = j + 1 # Display all "CC" recipients. j = 0 while j < email.NumCC : print("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. print(str(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. numAttach = imap.GetMailNumAttach(email) j = 0 while j < numAttach : # Display the attachment filename. print(imap.GetMailAttachFilename(email,j)) # Display the attachment size in bytes. attachSize = imap.GetMailAttachSize(email,j) print(" size = " + str(attachSize) + " bytes") j = j + 1 print("--") i = i + 1 # Disconnect from the IMAP server. success = imap.Disconnect() |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.