Swift
Swift
Copy Email from one IMAP Account to Another
See more IMAP Examples
Demonstrates how to copy the email in a mailbox from one account to another.Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
let imapSrc = CkoImap()!
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
// Connect to our source IMAP server.
imapSrc.ssl = true
imapSrc.port = 993
success = imapSrc.connect(hostname: "MY-IMAP-DOMAIN")
if success != true {
print("\(imapSrc.lastErrorText!)")
return
}
// Login to the source IMAP server
success = imapSrc.login(login: "MY-IMAP-LOGIN", password: "MY-IMAP-PASSWORD")
if success != true {
print("\(imapSrc.lastErrorText!)")
return
}
let imapDest = CkoImap()!
// Connect to our destination IMAP server.
imapDest.ssl = true
imapDest.port = 993
success = imapDest.connect(hostname: "MY-IMAP-DOMAIN2")
if success != true {
print("\(imapDest.lastErrorText!)")
return
}
// Login to the destination IMAP server
success = imapDest.login(login: "MY-IMAP-LOGIN2", password: "MY-IMAP-PASSWORD2")
if success != true {
print("\(imapDest.lastErrorText!)")
return
}
// Select a source IMAP mailbox on the source IMAP server
success = imapSrc.selectMailbox(mailbox: "Inbox")
if success != true {
print("\(imapSrc.lastErrorText!)")
return
}
var fetchUids: Bool = true
// Get the set of UIDs for all emails on the source server.
var mset: CkoMessageSet? = imapSrc.search(criteria: "ALL", bUid: fetchUids)
if imapSrc.lastMethodSuccess != true {
print("\(imapSrc.lastErrorText!)")
return
}
// Load the complete set of UIDs that were previously copied.
// We dont' want to copy any of these to the destination.
let fac = CkoFileAccess()!
let msetAlreadyCopied = CkoMessageSet()!
var strMsgSet: String? = fac.readEntireTextFile(path: "qa_cache/saAlreadyLoaded.txt", charset: "utf-8")
if fac.lastMethodSuccess == true {
msetAlreadyCopied.fromCompactString(str: strMsgSet)
}
var numUids: Int = mset!.count.intValue
let sbFlags = CkoStringBuilder()!
var i: Int = 0
while i < numUids {
// If this UID was not already copied...
var uid: Int = mset!.getId(index: i)
if !msetAlreadyCopied.containsId(id: uid) {
print("copying \(uid)...")
// Get the flags.
var flags: String? = imapSrc.fetchFlags(msgId: uid, bUid: true)
if imapSrc.lastMethodSuccess == false {
print("\(imapSrc.lastErrorText!)")
return
}
sbFlags.setString(value: flags)
// Get the MIME of this email from the source.
var mimeStr: String? = imapSrc.fetchSingle(asMime: uid, bUid: true)
if imapSrc.lastMethodSuccess == false {
print("\(imapSrc.lastErrorText!)")
return
}
var seen: Bool = sbFlags.contains(str: "\\Seen", caseSensitive: false)
var flagged: Bool = sbFlags.contains(str: "\\Flagged", caseSensitive: false)
var answered: Bool = sbFlags.contains(str: "\\Answered", caseSensitive: false)
var draft: Bool = sbFlags.contains(str: "\\Draft", caseSensitive: false)
success = imapDest.appendMime(withFlags: "Inbox", mimeText: mimeStr, seen: seen, flagged: flagged, answered: answered, draft: draft)
if success != true {
print("\(imapDest.lastErrorText!)")
return
}
// Update msetAlreadyCopied with the uid just copied.
msetAlreadyCopied.insertId(id: uid)
// Save at every iteration just in case there's a failure..
strMsgSet = msetAlreadyCopied.toCompactString()
fac.writeEntireTextFile(path: "qa_cache/saAlreadyLoaded.txt", fileData: strMsgSet, charset: "utf-8", includePreamble: false)
}
i = i + 1
}
mset = nil
// Disconnect from the IMAP servers.
success = imapSrc.disconnect()
success = imapDest.disconnect()
}