Sample code for 30+ languages & platforms
DataFlex

Append MIME with Initial IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its initial system flags. The first argument is the mailbox and the second is the MIME text. The third through sixth arguments control \Seen, \Flagged, \Answered, and \Draft respectively — pass true to set a flag. This example uploads a message that is already marked read.

Background: When importing or archiving mail you frequently want the uploaded message to arrive in a particular state — already read, pre-flagged, or marked as a draft. Setting the flags at append time is a single operation, avoiding a second STORE round trip. These explicit arguments take precedence over the AppendSeen property.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Handle hoEmail
    String sMimeText
    Boolean iSeen
    Boolean iFlagged
    Boolean iAnswered
    Boolean iDraft
    String sTemp1
    Integer iTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.AppendMimeWithFlags method, which uploads a MIME message and sets its
    //  initial system flags.  The 1st argument is the mailbox, the 2nd is the MIME text, and the
    //  3rd through 6th arguments set the \Seen, \Flagged, \Answered, and \Draft flags.

    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

    //  Build the email to be uploaded.
    Get Create (RefClass(cComChilkatEmail)) To hoEmail
    If (Not(IsComObjectCreated(hoEmail))) Begin
        Send CreateComObject of hoEmail
    End
    Set ComSubject Of hoEmail To "Meeting agenda"
    Set ComFrom Of hoEmail To "Alice <alice@example.com>"
    Get ComAddTo Of hoEmail "Bob" "bob@example.com" To iSuccess
    Set ComBody Of hoEmail To "Let's meet at 10am to review the agenda."

    Get ComGetMime Of hoEmail To sMimeText
    Get ComLastMethodSuccess Of hoEmail To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoEmail To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
    Move True To iSeen
    Move False To iFlagged
    Move False To iAnswered
    Move False To iDraft

    //  Upload to the Inbox already marked as read (\Seen), with the other flags unset.
    Get ComAppendMimeWithFlags Of hoImap "Inbox" sMimeText iSeen iFlagged iAnswered iDraft To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComAppendUid Of hoImap To iTemp1
    Showln "Appended UID: " iTemp1

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



End_Procedure