Swift
Swift
Fetch Oldest/Newest IMAP Email
Emails may be downloaded by sequence number. Assuming the selected mailbox is not empty, the oldest email is at sequence number 1, and the newest email is at sequence number N. The FetchSingle method may be used to download by sequence number.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes 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(hostname: "mail.testemail.net")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Login
success = imap.login(login: "***", password: "***")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Select an IMAP mailbox
success = imap.selectMailbox(mailbox: "Inbox")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// After selecting a mailbox, the NumMessages property
// contains the number of emails in the selected mailbox.
var n: Int
n = imap.numMessages.intValue
if n > 0 {
// The oldest email is always at sequence number 1.
var isUid: Bool = false
let oldestEmail = CkoEmail()!
success = imap.fetchEmail(headerOnly: false, msgId: 1, bUid: isUid, email: oldestEmail)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Display the From and Subject
print("\(oldestEmail.fromAddress!)")
print("\(oldestEmail.subject!)")
print("--")
// The newest email is at sequence number N:
let newestEmail = CkoEmail()!
success = imap.fetchEmail(headerOnly: false, msgId: n, bUid: isUid, email: newestEmail)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Display the From and Subject
print("\(newestEmail.fromAddress!)")
print("\(newestEmail.subject!)")
}
// Disconnect from the IMAP server.
success = imap.disconnect()
}