Swift
Swift
Get IMAP UID from Email Header
See more IMAP Examples
Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.Chilkat Swift Downloads
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(hostname: "imap.example.com")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Login
success = imap.login(login: "login", password: "password")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Select an IMAP mailbox
success = imap.selectMailbox(mailbox: "Inbox")
if success == false {
print("\(imap.lastErrorText!)")
return
}
var fetchUids: Bool = true
// Get the message UIDs of all the emails in the mailbox
let messageSet = CkoMessageSet()!
success = imap.queryMbx(criteria: "ALL", bUid: fetchUids, msgSet: messageSet)
if success == false {
print("\(imap.lastErrorText!)")
return
}
let bundle = CkoEmailBundle()!
var headersOnly: Bool = true
success = imap.fetchMsgSet(headersOnly: headersOnly, msgSet: messageSet, bundle: bundle)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// The UID of each fetched email header is available
// in the ckx-imap-uid header field.
let email = CkoEmail()!
let msgSet1 = CkoMessageSet()!
var i: Int = 0
var szBundle: Int = bundle.messageCount.intValue
while i < szBundle {
bundle.email(at: i, email: email)
// Build a message set containing one UID
var uidStr: String? = email.getHeaderField(fieldName: "ckx-imap-uid")
msgSet1.fromCompactString(str: uidStr)
// Move this message to some other folder.
success = imap.moveMessages(messageSet: msgSet1, destFolder: "someOtherFolder")
if success != true {
print("\(imap.lastErrorText!)")
// Exit the loop...
i = szBundle
}
i = i + 1
}
// Disconnect from the IMAP server.
success = imap.disconnect()
}