Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

Fetch Inbox Email Headers

Downloads the headers of all emails in the Inbox and shows some information about each, such as From, Subject, and whether the email has been seen (already read) or not.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nCount
LOCAL oBundle
LOCAL oEmailHeader
LOCAL i
LOCAL nSeenFlag

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("myLogin", "myPassword")
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

//  Normally, when an email is downloaded, its "Seen" flag is automatically set.
//  We don't want our program to be interfering with the "Seen" flags.
//  To do this, set the PeekMode to 1.
oImap:PeekMode := 1

//  After selecting the mailbox, the NumMessages property
//  will contain the number of emails in the mailbox.
//  When sequence numbers (not UIDs) are used to reference emails,
//  they range from 1 to N, where N is the number of messages in the mailbox.
//  This example will download the headers by sequence numbers.
nCount := oImap:NumMessages

oBundle := CreateObject("Chilkat.EmailBundle")
nSuccess := oImap:FetchRange(1, 1, nCount, oBundle)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oBundle:destroy()
    RETURN
ENDIF

IF (oBundle:MessageCount == 0)
    ? "No messages in Inbox."
    oImap:destroy()
    oBundle:destroy()
    RETURN
ENDIF

//  Loop over the email headers.  
oEmailHeader := CreateObject("Chilkat.Email")
i := 0
DO WHILE i < oBundle:MessageCount

    oBundle:EmailAt(i, oEmailHeader)

    ? oEmailHeader:From
    ? oEmailHeader:Subject

    //  Get the "Seen" flag.
    //  When an email is fetched from the IMAP server, the email flags are stored
    //  within the email in the "ckx-imap-flags" header.
    //  The call to GetMailFlag is simply getting the information from the email header.
    //  (it is not communicating with the IMAP server to get this information, because
    //  the information was obtained when downloading the headers)
    nSeenFlag := oImap:GetMailFlag(oEmailHeader, "Seen")
    IF (nSeenFlag == 1)
        ? "This email has been read (already seen)"
    ELSE
        ? "This email has not yet been read (it is not yet seen)"
    ENDIF

    //  We can also look directly at the "ckx-imap-flags" header:
    ? "ckx-imap-flags: " + oEmailHeader:GetHeaderField("ckx-imap-flags")

    ? "--"

    i := i + 1
ENDDO

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

oImap:destroy()
oBundle:destroy()
oEmailHeader:destroy()