Sample code for 30+ languages & platforms
Swift

Append MIME with Initial IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its initial system flags. The first argument is the mailbox and the second is the MIME text. The third through sixth arguments control \Seen, \Flagged, \Answered, and \Draft respectively — pass true to set a flag. This example uploads a message that is already marked read.

Background: When importing or archiving mail you frequently want the uploaded message to arrive in a particular state — already read, pre-flagged, or marked as a draft. Setting the flags at append time is a single operation, avoiding a second STORE round trip. These explicit arguments take precedence over the AppendSeen property.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Demonstrates the Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its
    //  initial system flags.  The 1st argument is the mailbox, the 2nd is the MIME text, and the
    //  3rd through 6th arguments set the \Seen, \Flagged, \Answered, and \Draft flags.

    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."

    var mimeText: String? = email.getMime()
    if email.lastMethodSuccess == false {
        print("\(email.lastErrorText!)")
        return
    }

    //  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
    var seen: Bool = true
    var flagged: Bool = false
    var answered: Bool = false
    var draft: Bool = false

    //  Upload to the Inbox already marked as read (\Seen), with the other flags unset.
    success = imap.appendMime(withFlags: "Inbox", mimeText: mimeText, seen: seen, flagged: flagged, answered: answered, draft: draft)
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    print("Appended UID: \(imap.appendUid.intValue)")

    success = imap.disconnect()
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }


}