Sample code for 30+ languages & platforms
PureBasic

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 PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkEmailBundle.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)

    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"user@example.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    msgSet.i = CkMessageSet::ckCreate()
    If msgSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,"ALL",1,msgSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(msgSet)
        ProcedureReturn
    EndIf

    bundle.i = CkEmailBundle::ckCreate()
    If bundle.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckFetchMsgSet(imap,1,msgSet,bundle)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(msgSet)
        CkEmailBundle::ckDispose(bundle)
        ProcedureReturn
    EndIf

    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    If CkEmailBundle::ckMessageCount(bundle) > 0
        success = CkEmailBundle::ckEmailAt(bundle,0,email)

        ;  Re-read the flags from the server, in case another client changed them since the fetch.
        success = CkImap::ckRefetchMailFlags(imap,email)
        If success = 0
            Debug CkImap::ckLastErrorText(imap)
            CkImap::ckDispose(imap)
            CkMessageSet::ckDispose(msgSet)
            CkEmailBundle::ckDispose(bundle)
            CkEmail::ckDispose(email)
            ProcedureReturn
        EndIf

        seen.i = CkImap::ckGetMailFlag(imap,email,"Seen")
        Debug "Current Seen state: " + Str(seen)
    EndIf

    success = CkImap::ckDisconnect(imap)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(msgSet)
        CkEmailBundle::ckDispose(bundle)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf



    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(msgSet)
    CkEmailBundle::ckDispose(bundle)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure