Sample code for 30+ languages & platforms
DataFlex

Set an IMAP Flag Using an Email Object

See more IMAP Examples

Demonstrates the Chilkat Imap.SetMailFlag method, which sets or clears a flag on the server for the message represented by an Email object. The first argument is the Email, the second is the flag name, and the third is the value (1 to set, 0 to clear). This example flags each fetched message as Flagged on the server.

Background: SetMailFlag is convenient when you already hold an Email from a fetch: Chilkat uses the UID it recorded on that object to target the right message on the server, so you do not have to track IDs yourself. It updates both the server and the local Email object, keeping the two in sync. Under the hood this is the same STORE operation as SetFlag — just addressed by object rather than by raw ID.

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 i
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.SetMailFlag method, which sets or clears a flag on the server for the
    //  message represented by an Email object.  The 1st argument is the Email, the 2nd is the flag
    //  name, and the 3rd is the value (1 to set, 0 to clear).

    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 "UNSEEN" 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
    For i From 0 To (iTemp1 - 1)
        Get pvComObject of hoEmail to vEmail
        Get ComEmailAt Of hoBundle i vEmail To iSuccess
        //  Mark this message as Flagged on the server.  A value of 1 sets the flag; 0 would clear it.
        Get pvComObject of hoEmail to vEmail
        Get ComSetMailFlag Of hoImap vEmail "Flagged" 1 To iSuccess
        If (iSuccess = False) Begin
            Get ComLastErrorText Of hoImap To sTemp1
            Showln sTemp1
            Procedure_Return
        End

    Loop

    Get ComMessageCount Of hoBundle To iTemp1
    Showln "Flagged " iTemp1 " messages."

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



End_Procedure