Sample code for 30+ languages & platforms
B4X

Check for IMAP IDLE Mailbox Updates

See more IMAP Examples

Demonstrates the Chilkat Imap.IdleCheck method, which waits for mailbox updates after IdleStart has entered IDLE mode. The only argument is a timeout in milliseconds; 0 performs a non-blocking poll of notifications already received. This example waits up to 30 seconds.

Background: IdleCheck consumes the notifications already waiting on the connection rather than sending a new polling command. A typical monitoring loop repeatedly calls IdleCheck with a timeout and reacts when updates arrive. Because servers usually drop an idle connection after about 29 minutes, long-lived monitors periodically call IdleDone and then IdleStart again to refresh it.

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

'  Demonstrates the Imap.IdleCheck method, which waits for IMAP IDLE mailbox updates and returns
'  them.  The only argument is a timeout in milliseconds.  Use 0 for a non-blocking poll.

Dim imap As ChilkatImap
imap.Initialize("imap")

imap.Ssl = True
imap.Port = 993

success = imap.Connect("imap.example.com")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If

success = imap.Login("user@example.com", "myPassword")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


success = imap.SelectMailbox("Inbox")
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


success = imap.IdleStart
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


'  Wait up to 30 seconds for mailbox updates.  IdleCheck returns a string, so assign it to a
'  string variable and check for success.
Dim timeoutMs As Int = 30000
Dim updates As String = imap.IdleCheck(timeoutMs)
If imap.LastMethodSuccess = False Then
    Log(imap.LastErrorText)
    Return
End If

Log(updates)

success = imap.IdleDone
If success = False Then
    Log(imap.LastErrorText)
    Return
End If


success = imap.Disconnect
If success = False Then
    Log(imap.LastErrorText)
    Return
End If