Sample code for 30+ languages & platforms
Swift

Get an IMAP Flag from a Downloaded Email

See more IMAP Examples

Demonstrates the Chilkat Imap.GetMailFlag method, which returns the state of a named flag for an Email object that was downloaded from the server. The first argument is the Email and the second is the flag name. The return value is 1 if the flag is set, 0 if not, and -1 if the flag is not present. This example iterates a bundle and reports which messages are read vs unread.

Background: When Chilkat downloads a message it records the flags the server reported at fetch time, so you can inspect them locally with GetMailFlag without another round trip. This is the client-side view: it reflects the state as of the download, not necessarily the live state on the server, which other clients could have changed. Use RefetchMailFlags to update an Email object's flags to the current server values.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    //  Demonstrates the Imap.GetMailFlag method, which returns the state of a named flag for an
    //  Email object that was downloaded from the server.  The 1st argument is the Email and the
    //  2nd is the flag name.  The return value is 1 if the flag is set, 0 if not, and -1 if the
    //  flag is not present.

    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()!
    var i: Int
    for i = 0; i <= bundle.messageCount.intValue - 1; i++ {
        success = bundle.email(at: i, email: email)
        //  Check whether each message has been read (the "Seen" flag).
        var seen: Int = imap.getMailFlag(email: email, flagName: "Seen").intValue
        if seen == 1 {
            print("\(email.subject!) -- already read")
        }

ERROR: "}" expected
    }

}