Sample code for 30+ languages & platforms
Swift

Refresh an Email Object's IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.RefetchMailFlags method, which updates the flags stored on an Email object to the current values on the server. The only argument is the Email object. This example re-reads the flags for a previously fetched message and then checks its Seen state.

Background: Flags are shared mailbox state, so between the time you fetch a message and the time you act on it, another client (a phone, a webmail tab) may have marked it read, flagged, or deleted. RefetchMailFlags pulls just the flags — not the whole message — so a long-running job can cheaply re-check the live state before deciding what to do, avoiding acting on stale information.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Demonstrates the Imap.RefetchMailFlags method, which updates the flags stored on an Email
    //  object to the current values on the server.  The only argument is the Email object.

    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
    }

    success = imap.selectMailbox(mailbox: "Inbox")
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    let msgSet = CkoMessageSet()!
    success = imap.queryMbx(criteria: "ALL", bUid: true, msgSet: msgSet)
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    let bundle = CkoEmailBundle()!
    success = imap.fetchMsgSet(headersOnly: true, msgSet: msgSet, bundle: bundle)
    if success == false {
        print("\(imap.lastErrorText!)")
        return
    }

    let email = CkoEmail()!
    if bundle.messageCount.intValue > 0 {
        success = bundle.email(at: 0, email: email)

        //  Re-read the flags from the server, in case another client changed them since the fetch.
        success = imap.refetchMailFlags(emailInOut: email)
        if success == false {
            print("\(imap.lastErrorText!)")
            return
        }

        var seen: Int = imap.getMailFlag(email: email, flagName: "Seen").intValue
        print("Current Seen state: \(seen)")
    }

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


}