Sample code for 30+ languages & platforms
DataFlex

Start IMAP IDLE to Monitor a Mailbox

See more IMAP Examples

Demonstrates the Chilkat Imap.IdleStart method, which sends the IMAP IDLE command to begin listening for unsolicited mailbox updates. It takes no arguments. A mailbox must first be selected, and the server must advertise the IDLE capability.

Background: IDLE is IMAP's push mechanism: instead of polling for new mail, the client tells the server "notify me when something changes," and the server pushes updates as they happen. This is what enables near-instant new-mail alerts. While IDLE is active, no other IMAP command may be sent on that connection — call IdleDone to leave IDLE before issuing other commands.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoImap
    String sTemp1

    Move False To iSuccess

    //  Demonstrates the Imap.IdleStart method, which sends the IMAP IDLE command to begin listening
    //  for unsolicited mailbox updates.  It takes no arguments.  A mailbox must be selected and the
    //  server must advertise the IDLE capability.

    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

    //  Enter IDLE mode to monitor the mailbox for changes (new mail, deletions, flag changes).
    Get ComIdleStart Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "IDLE mode started."

    //  While IDLE is active, no other IMAP command may be sent.  End IDLE before doing more work.
    Get ComIdleDone Of hoImap To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoImap To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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



End_Procedure