Sample code for 30+ languages & platforms
PureBasic

Download Unread Email from IMAP Mailbox

Download unread email messages from an IMAP mailbox.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkImap.pb"
IncludeFile "CkEmailBundle.pb"
IncludeFile "CkMessageSet.pb"
IncludeFile "CkEmail.pb"

Procedure ChilkatExample()

    success.i = 0

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

    imap.i = CkImap::ckCreate()
    If imap.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Connect to an IMAP server.
    ; Use TLS
    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Login
    success = CkImap::ckLogin(imap,"admin@chilkatsoft.com","myPassword")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Select an IMAP mailbox
    success = CkImap::ckSelectMailbox(imap,"Inbox")
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    ; Find emails marked as seen or not already seen:
    seenSearch.s = "SEEN"
    notSeenSearch.s = "NOT SEEN"

    ; Get the set of unseen message UIDs
    fetchUids.i = 1
    messageSet.i = CkMessageSet::ckCreate()
    If messageSet.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckQueryMbx(imap,notSeenSearch,fetchUids,messageSet)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        ProcedureReturn
    EndIf

    ; Fetch the unseen emails into a bundle object:
    bundle.i = CkEmailBundle::ckCreate()
    If bundle.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    headersOnly.i = 0
    success = CkImap::ckFetchMsgSet(imap,headersOnly,messageSet,bundle)
    If success = 0
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(messageSet)
        CkEmailBundle::ckDispose(bundle)
        ProcedureReturn
    EndIf

    ; Display the Subject and From of each email.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    i.i = 0
    numEmails.i = CkEmailBundle::ckMessageCount(bundle)
    While i < numEmails
        CkEmailBundle::ckEmailAt(bundle,i,email)

        Debug CkEmail::ckGetHeaderField(email,"Date")
        Debug CkEmail::ckSubject(email)
        Debug CkEmail::ckFrom(email)
        Debug "--"
        i = i + 1
    Wend

    ; Disconnect from the IMAP server.
    success = CkImap::ckDisconnect(imap)


    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(messageSet)
    CkEmailBundle::ckDispose(bundle)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure