Swift
Swift
Copy an Email from One Mailbox to Another
Copies an email from one IMAP folder to another. After running this example, copies of the email will be present in both source and destination folders.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.
// This example copies an email from one mailbox to another.
let imap = CkoImap()!
// Turn on session logging:
imap.keepSessionLog = true
// 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: "***", password: "***")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Select an IMAP mailbox
success = imap.selectMailbox(mailbox: "Inbox.testing.a")
if success == false {
print("\(imap.lastErrorText!)")
return
}
var fetchUids: Bool = true
// Get the message IDs for all emails having "Re:" in the subject.
let messageSet = CkoMessageSet()!
success = imap.queryMbx(criteria: "SUBJECT Re:", bUid: fetchUids, msgSet: messageSet)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Copy the messages from "Inbox.testing.a" to "Inbox.testing.b" in one call to CopyMultiple:
success = imap.copyMultiple(messageSet: messageSet, copyToMailbox: "Inbox.testing.b")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Alternatively, loop over each message in the set and
// copy each separately:
var i: Int = 0
while i < messageSet.count.intValue {
var msgId: Int = messageSet.getId(index: i)
var isUid: Bool = messageSet.hasUids
success = imap.copy(msgId: msgId, bUid: isUid, copyToMailbox: "Inbox.testing.c")
if success == false {
print("\(imap.lastErrorText!)")
return
}
i = i + 1
}
// Display the session log.
print("\(imap.sessionLog!)")
// Disconnect from the IMAP server.
success = imap.disconnect()
}