Swift
Swift
Fetch a MessageSet into an EmailBundle
See more IMAP Examples
Demonstrates the Chilkat Imap.FetchMsgSet method, which downloads the messages identified by a MessageSet and appends them to an EmailBundle. The arguments are headersOnly, the MessageSet, and the EmailBundle (which is not cleared). This example searches for unseen messages and downloads the whole set.
Background: This is the natural pairing with
QueryMbx: search to get a MessageSet of matching identifiers, then download exactly those messages in one call. Fetching a set is far more efficient than looping one message at a time, and it returns an EmailBundle you iterate with MessageCount and EmailAt. Pass headersOnly = true to pull just headers for a fast preview list.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Demonstrates the Imap.FetchMsgSet method, which downloads the messages identified by a
// MessageSet and appends them to an EmailBundle. The 1st argument is headersOnly, the 2nd
// is the MessageSet, and the 3rd is the EmailBundle (which is not cleared).
let imap = CkoImap()!
imap.ssl = true
imap.port = 993
success = imap.connect(hostname: "imap.example.com")
if success == false {
print("\(imap.lastErrorText!)")
return
}
success = imap.login(login: "user@example.com", password: "myPassword")
if success == false {
print("\(imap.lastErrorText!)")
return
}
success = imap.selectMailbox(mailbox: "Inbox")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Find the messages to download (here, the unseen messages by UID).
let msgSet = CkoMessageSet()!
success = imap.queryMbx(criteria: "UNSEEN", bUid: true, msgSet: msgSet)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Download the messages in the set (full bodies) into a bundle.
let bundle = CkoEmailBundle()!
success = imap.fetchMsgSet(headersOnly: false, msgSet: msgSet, bundle: bundle)
if success == false {
print("\(imap.lastErrorText!)")
return
}
var n: Int = bundle.messageCount.intValue
print("Fetched \(n) messages.")
let email = CkoEmail()!
var i: Int
for i = 0; i <= n - 1; i++ {
success = bundle.email(at: i, email: email)
print("\(email.subject!)")
}
success = imap.disconnect()
if success == false {
print("\(imap.lastErrorText!)")
return
}
}