![]() |
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
(Tcl) 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.
load ./chilkat.dll set success 0 set success 0 # This example assumes the Chilkat API has already been unlocked. # See Global Unlock Sample for example code. set imap [new_CkImap] # Connect to the IMAP server using SSL/TLS on the standard secure IMAP port. CkImap_put_Ssl $imap 1 CkImap_put_Port $imap 993 set success [CkImap_Connect $imap "imap.example.com"] if {$success == 0} then { puts [CkImap_lastErrorText $imap] delete_CkImap $imap exit } # Authenticate with the IMAP server. set success [CkImap_Login $imap "****" "****"] if {$success == 0} then { puts [CkImap_lastErrorText $imap] delete_CkImap $imap exit } # Select the mailbox (folder) to access. set success [CkImap_SelectMailbox $imap "Inbox"] if {$success == 0} then { puts [CkImap_lastErrorText $imap] delete_CkImap $imap 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. set fetchUids 1 set messageSet [new_CkMessageSet] set success [CkImap_QueryMbx $imap "ALL" $fetchUids $messageSet] if {$success == 0} then { puts [CkImap_lastErrorText $imap] delete_CkImap $imap delete_CkMessageSet $messageSet 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. set bundle [new_CkEmailBundle] set headersOnly 1 set success [CkImap_FetchMsgSet $imap $headersOnly $messageSet $bundle] if {$success == 0} then { puts [CkImap_lastErrorText $imap] delete_CkImap $imap delete_CkMessageSet $messageSet delete_CkEmailBundle $bundle exit } # Iterate over each email in the bundle and display summary information. set email [new_CkEmail] set i 0 set j 0 while {$i < [CkEmailBundle_get_MessageCount $bundle]} { CkEmailBundle_EmailAt $bundle $i $email # Display the sender and subject line. puts [CkEmail_from $email] puts [CkEmail_subject $email] # Display all "To" recipients. set j 0 while {$j < [CkEmail_get_NumTo $email]} { puts "TO: [CkEmail_getToName $email $j], [CkEmail_getToAddr $email $j]" set j [expr $j + 1] } # Display all "CC" recipients. set j 0 while {$j < [CkEmail_get_NumCC $email]} { puts "CC: [CkEmail_getCcName $email $j], [CkEmail_getCcAddr $email $j]" set j [expr $j + 1] } # Display the total size of the email message, including # the body and all attachments, even though only headers # were downloaded. puts [CkEmail_get_Size $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. set numAttach [CkImap_GetMailNumAttach $imap $email] set j 0 while {$j < $numAttach} { # Display the attachment filename. puts [CkImap_getMailAttachFilename $imap $email $j] # Display the attachment size in bytes. set attachSize [CkImap_GetMailAttachSize $imap $email $j] puts " size = $attachSize bytes" set j [expr $j + 1] } puts "--" set i [expr $i + 1] } # Disconnect from the IMAP server. set success [CkImap_Disconnect $imap] delete_CkImap $imap delete_CkMessageSet $messageSet delete_CkEmailBundle $bundle delete_CkEmail $email |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.