Swift
Swift
Imap.GetMailSize vs Email.Size
Shows how to get the total size of an email, as well as the sizes of the attachments. This can be done when either full-emails or headers-only are downloaded.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: "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")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Get the message IDs of all the emails in the mailbox
// We can choose to fetch UIDs or sequence numbers.
var fetchUids: Bool = true
let messageSet = CkoMessageSet()!
success = imap.queryMbx(criteria: "ALL", bUid: fetchUids, msgSet: messageSet)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// When downloading headers, each email object contains
// (obviously) the headers, but the body will be missing.
// Attachments will not be included. However, it is
// possible to get information about the attachments
// as well as the complete size of the email.
let bundle = CkoEmailBundle()!
var headersOnly: Bool = true
success = imap.fetchMsgSet(headersOnly: headersOnly, msgSet: messageSet, bundle: bundle)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Loop over the email objects and display information about each:
let email = CkoEmail()!
var i: Int = 0
var j: Int = 0
var numEmails: Int = bundle.messageCount.intValue
while i < numEmails {
bundle.email(at: i, email: email)
// Display the From and Subject
print("\(email.from!)")
print("\(email.subject!)")
// Display the recipients:
j = 0
while j < email.numTo.intValue {
print("TO: \(email.get(toName: j)!), \(email.get(toAddr: j)!)")
j = j + 1
}
j = 0
while j < email.numCC.intValue {
print("CC: \(email.getCcName(index: j)!), \(email.getCcAddr(index: j)!)")
j = j + 1
}
// Show the total size of the email, including body and attachments:
print("\(email.size.intValue)")
// When a full email is downloaded, we would use the
// email.NumAttachments property in conjunction with the
// email.GetMailAttach* methods.
// However, when an email object contains only the header,
// we need to access the attachment info differently:
var numAttach: Int = imap.getMailNumAttach(email: email).intValue
j = 0
while j < numAttach {
print("\(imap.getMailAttachFilename(email: email, attachIndex: j)!)")
var attachSize: Int = imap.getMailAttachSize(email: email, attachIndex: j).intValue
print(" size = \(attachSize) bytes")
j = j + 1
}
print("--")
i = i + 1
}
// Disconnect from the IMAP server.
success = imap.disconnect()
}