![]() |
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
(Swift) Read IMAP Email HeadersCall FetchHeaders to download only the email headers. Note: This example requires Chilkat v11.0.0 or greater.
func chilkatTest() { var success: Bool = false // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. let imap = CkoImap()! // Connect to an IMAP server. // Use TLS imap.ssl = true imap.port = 993 success = imap.connect("imap.example.com") if success == false { print("\(imap.lastErrorText!)") return } // Login success = imap.login("****", password: "****") if success == false { print("\(imap.lastErrorText!)") return } // Select an IMAP mailbox success = imap.selectMailbox("Inbox") if success == false { print("\(imap.lastErrorText!)") return } // Get the message IDs of all the emails in the mailbox // We can choose to fetch UIDs or sequence numbers. var fetchUids: Bool = true let messageSet = CkoMessageSet()! success = imap.queryMbx("ALL", bUid: fetchUids, msgSet: messageSet) if success == false { print("\(imap.lastErrorText!)") 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. let bundle = CkoEmailBundle()! var headersOnly: Bool = true success = imap.fetchMsgSet(headersOnly, msgSet: messageSet, bundle: bundle) if success == false { print("\(imap.lastErrorText!)") return } // Loop over the email objects and display information // about each: let email = CkoEmail()! var name: String? var addr: String? var i: Int = 0 var j: Int = 0 while i < bundle.messageCount.intValue { bundle.email(at: i, email: email) // Display the From and Subject print("\(email.from!)") print("\(email.subject!)") // Display the recipients: j = 0 while j < email.numTo.intValue { print("TO: \(email.getToName(j)!), \(email.getToAddr(j)!)") j = j + 1 } j = 0 while j < email.numCC.intValue { print("CC: \(email.getCcName(j)!), \(email.getCcAddr(j)!)") j = j + 1 } // Show the total size of the email, including body and attachments: print("\(email.size.intValue)") // 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: var numAttach: Int = imap.getMailNumAttach(email).intValue j = 0 while j < numAttach { print("\(imap.getMailAttachFilename(email, attachIndex: j)!)") var attachSize: Int = imap.getMailAttachSize(email, attachIndex: j).intValue print(" size = \(attachSize) bytes") j = j + 1 } print("--") } // Disconnect from the IMAP server. success = imap.disconnect() } |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.