Sample code for 30+ languages & platforms
DataFlex

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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Variant vMsgSet
    Handle hoMsgSet
    Variant vBundle
    Handle hoBundle
    Variant vEmail
    Handle hoEmail
    Integer iSeen
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatImap)) To hoImap
    If (Not(IsComObjectCreated(hoImap))) Begin
        Send CreateComObject of hoImap
    End

    Set ComSsl Of hoImap To True
    Set ComPort Of hoImap To 993

    Get ComConnect Of hoImap "imap.example.com" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComLogin Of hoImap "user@example.com" "myPassword" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatMessageSet)) To hoMsgSet
    If (Not(IsComObjectCreated(hoMsgSet))) Begin
        Send CreateComObject of hoMsgSet
    End
    Get pvComObject of hoMsgSet to vMsgSet
    Get ComQueryMbx Of hoImap "ALL" True vMsgSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatEmailBundle)) To hoBundle
    If (Not(IsComObjectCreated(hoBundle))) Begin
        Send CreateComObject of hoBundle
    End
    Get pvComObject of hoMsgSet to vMsgSet
    Get pvComObject of hoBundle to vBundle
    Get ComFetchMsgSet Of hoImap True vMsgSet vBundle To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Get ComMessageCount Of hoBundle To iTemp1
    If (iTemp1 > 0) Begin
        Get pvComObject of hoEmail to vEmail
        Get ComEmailAt Of hoBundle 0 vEmail To iSuccess

        //  Re-read the flags from the server, in case another client changed them since the fetch.
        Get pvComObject of hoEmail to vEmail
        Get ComRefetchMailFlags Of hoImap vEmail To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoImap To sTemp1
            Showln sTemp1
            Procedure_Return
        End

        Get pvComObject of hoEmail to vEmail
        Get ComGetMailFlag Of hoImap vEmail "Seen" To iSeen
        Showln "Current Seen state: " iSeen
    End

    Get ComDisconnect Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End



End_Procedure