Sample code for 30+ languages & platforms
Xbase++

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nTotalNum
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL nNumUnseen
LOCAL i

nSuccess := 0

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

oImap := CreateObject("Chilkat.Imap")

//  Connect to an IMAP server.
//  Use TLS
oImap:Ssl := 1
oImap:Port := 993
nSuccess := oImap:Connect("imap.example.com")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Login
nSuccess := oImap:Login("***", "***")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  Select an IMAP mailbox
nSuccess := oImap:SelectMailbox("Inbox")
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    RETURN
ENDIF

//  After selecting the mailbox. the total number of emails
//  is immediately available:
nTotalNum := oImap:NumMessages
? "Num messages = " + Str(nTotalNum)

//  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.
nFetchUids := 1
oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("UNSEEN", nFetchUids, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

nNumUnseen := oMessageSet:Count
? Str(nNumUnseen)

? "UIDs ----"

//  Display the UIDs
i := 0
DO WHILE i < oMessageSet:Count
    ? Str(oMessageSet:GetId(i))
    i := i + 1
ENDDO

//  Disconnect from the IMAP server.
nSuccess := oImap:Disconnect()

oImap:destroy()
oMessageSet:destroy()