![]() |
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
(Objective-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.
#import <CkoImap.h> #import <CkoMessageSet.h> #import <CkoEmailBundle.h> #import <CkoEmail.h> #import <NSString.h> BOOL success = NO; success = NO; // This example assumes the Chilkat API has already been unlocked. // See Global Unlock Sample for example code. CkoImap *imap = [[CkoImap alloc] init]; // Connect to the IMAP server using SSL/TLS on the standard secure IMAP port. imap.Ssl = YES; imap.Port = [NSNumber numberWithInt:993]; success = [imap Connect: @"imap.example.com"]; if (success == NO) { NSLog(@"%@",imap.LastErrorText); return; } // Authenticate with the IMAP server. success = [imap Login: @"****" password: @"****"]; if (success == NO) { NSLog(@"%@",imap.LastErrorText); return; } // Select the mailbox (folder) to access. success = [imap SelectMailbox: @"Inbox"]; if (success == NO) { NSLog(@"%@",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 = YES; CkoMessageSet *messageSet = [[CkoMessageSet alloc] init]; success = [imap QueryMbx: @"ALL" bUid: fetchUids msgSet: messageSet]; if (success == NO) { NSLog(@"%@",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. CkoEmailBundle *bundle = [[CkoEmailBundle alloc] init]; BOOL headersOnly = YES; success = [imap FetchMsgSet: headersOnly msgSet: messageSet bundle: bundle]; if (success == NO) { NSLog(@"%@",imap.LastErrorText); return; } // Iterate over each email in the bundle and display summary information. CkoEmail *email = [[CkoEmail alloc] init]; NSString *name = 0; NSString *addr = 0; int i = 0; int j = 0; while (i < [bundle.MessageCount intValue]) { [bundle EmailAt: [NSNumber numberWithInt: i] email: email]; // Display the sender and subject line. NSLog(@"%@",email.From); NSLog(@"%@",email.Subject); // Display all "To" recipients. j = 0; while (j < [email.NumTo intValue]) { NSLog(@"%@%@%@%@",@"TO: ",[email GetToName: [NSNumber numberWithInt: j]],@", ",[email GetToAddr: [NSNumber numberWithInt: j]]); j = j + 1; } // Display all "CC" recipients. j = 0; while (j < [email.NumCC intValue]) { NSLog(@"%@%@%@%@",@"CC: ",[email GetCcName: [NSNumber numberWithInt: j]],@", ",[email GetCcAddr: [NSNumber numberWithInt: j]]); j = j + 1; } // Display the total size of the email message, including // the body and all attachments, even though only headers // were downloaded. NSLog(@"%d",[email.Size intValue]); // 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] intValue]; j = 0; while (j < numAttach) { // Display the attachment filename. NSLog(@"%@",[imap GetMailAttachFilename: email attachIndex: [NSNumber numberWithInt: j]]); // Display the attachment size in bytes. int attachSize = [[imap GetMailAttachSize: email attachIndex: [NSNumber numberWithInt: j]] intValue]; NSLog(@"%@%d%@",@" size = ",attachSize,@" bytes"); j = j + 1; } NSLog(@"%@",@"--"); i = i + 1; } // Disconnect from the IMAP server. success = [imap Disconnect]; |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.