Sample code for 30+ languages & platforms
DataFlex

Determine the Number of Unseen Email Messages

Demonstrates how to determine how many unseen messages exist in an email account on an IMAP server.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    Integer iTotalNum
    Boolean iFetchUids
    Variant vMessageSet
    Handle hoMessageSet
    UInteger iNumUnseen
    Integer i
    String sTemp1
    Integer iTemp1

    Move False To iSuccess

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

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

    // Connect to an IMAP server.
    // Use TLS
    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

    // Login
    Get ComLogin Of hoImap "***" "***" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Select an IMAP mailbox
    Get ComSelectMailbox Of hoImap "Inbox" To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // After selecting the mailbox. the total number of emails
    // is immediately available:
    Get ComNumMessages Of hoImap To iTotalNum
    Showln "Num messages = " iTotalNum

    // To determine the number of unseen messages, a call
    // to Search is required, which returns the set of UIDs
    // of all unseen messages.

    // We can choose to fetch UIDs or sequence numbers.
    Move True To iFetchUids
    Get Create (RefClass(cComChilkatMessageSet)) To hoMessageSet
    If (Not(IsComObjectCreated(hoMessageSet))) Begin
        Send CreateComObject of hoMessageSet
    End
    Get pvComObject of hoMessageSet to vMessageSet
    Get ComQueryMbx Of hoImap "UNSEEN" iFetchUids vMessageSet To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComCount Of hoMessageSet To iNumUnseen
    Showln iNumUnseen

    Showln "UIDs ----"

    // Display the UIDs
    Move 0 To i
    While (i < (ComCount(hoMessageSet)))
        Get ComGetId Of hoMessageSet i To iTemp1
        Showln iTemp1
        Move (i + 1) To i
    Loop

    // Disconnect from the IMAP server.
    Get ComDisconnect Of hoImap To iSuccess


End_Procedure