Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkEmail.pb"
IncludeFile "CkImap.pb"

Procedure ChilkatExample()

    success.i = 0

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

    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

    ;  Build the email to be uploaded.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkEmail::setCkSubject(email, "Meeting agenda")
    CkEmail::setCkFrom(email, "Alice <alice@example.com>")
    success = CkEmail::ckAddTo(email,"Bob","bob@example.com")
    CkEmail::setCkBody(email, "Let's meet at 10am to review the agenda.")

    mimeText.s = CkEmail::ckGetMime(email)
    If CkEmail::ckLastMethodSuccess(email) = 0
        Debug CkEmail::ckLastErrorText(email)
        CkImap::ckDispose(imap)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ;  Choose the initial flags.  Named variables make each argument's meaning clear at the call.
    seen.i = 1
    flagged.i = 0
    answered.i = 0
    draft.i = 0

    ;  Upload to the Inbox already marked as read (\Seen), with the other flags unset.
    success = CkImap::ckAppendMimeWithFlags(imap,"Inbox",mimeText,seen,flagged,answered,draft)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "Appended UID: " + Str(CkImap::ckAppendUid(imap))

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



    CkImap::ckDispose(imap)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure