Swift
Swift
Append (Upload) an Email to an IMAP Mailbox
See more IMAP Examples
Demonstrates the Chilkat Imap.AppendMail method, which uploads an Email object to a mailbox on the server. The first argument is the destination mailbox name and the second is the Email. After a successful append, the AppendUid property holds the UID the server assigned (or 0 if none was reported). This example uploads a newly built message to the Drafts mailbox.
Background: The IMAP
APPEND command lets a client add a message directly to any mailbox without sending it through SMTP — it is how "Save to Drafts" works, and how a client keeps its own copy in Sent. The initial \Seen state is governed by the AppendSeen property (or by the flag arguments of the AppendMimeWithFlags variants).Chilkat Swift Downloads
func chilkatTest() {
var success: Bool = false
// Demonstrates the Imap.AppendMail method, which uploads an Email object to a mailbox on the
// server. The 1st argument is the destination mailbox name and the 2nd is the Email.
let imap = CkoImap()!
imap.ssl = true
imap.port = 993
success = imap.connect(hostname: "imap.example.com")
if success == false {
print("\(imap.lastErrorText!)")
return
}
success = imap.login(login: "user@example.com", password: "myPassword")
if success == false {
print("\(imap.lastErrorText!)")
return
}
// Build the email to be uploaded.
let email = CkoEmail()!
email.subject = "Meeting agenda"
email.from = "Alice <alice@example.com>"
success = email.add(to: "Bob", emailAddress: "bob@example.com")
email.body = "Let's meet at 10am to review the agenda."
// Append (upload) the email to the "Drafts" mailbox.
success = imap.appendMail(mailbox: "Drafts", email: email)
if success == false {
print("\(imap.lastErrorText!)")
return
}
// AppendUid holds the UID assigned by the server (0 if the server did not report one).
print("Appended UID: \(imap.appendUid.intValue)")
success = imap.disconnect()
if success == false {
print("\(imap.lastErrorText!)")
return
}
}