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

Find an email by subject and fetch it by IMAP UID

See more Email Object Examples

Search the selected INBOX by subject and fetch the first matching UID into an Email. The example requests complete attachment bodies and uses peek mode to preserve the read flag. Configure your own IMAP TLS endpoint and credentials.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ;  Assumes Chilkat is unlocked. See Global Unlock Sample.

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

    CkImap::setCkSsl(imap, 1)
    CkImap::setCkPort(imap, 993)
    CkImap::setCkRequireSslCertVerify(imap, 1)
    CkImap::setCkPeekMode(imap, 1)
    CkImap::setCkAutoDownloadAttachments(imap, 1)
    success = CkImap::ckConnect(imap,"imap.example.com")
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckLogin(imap,"MAIL_USER","MAIL_PASSWORD")
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

    success = CkImap::ckSelectMailbox(imap,"INBOX")
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        ProcedureReturn
    EndIf

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

    success = CkImap::ckQueryMbx(imap,"SUBJECT " + Chr(34) + "Sample message" + Chr(34),1,matches)
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(matches)
        ProcedureReturn
    EndIf

    If CkMessageSet::ckCount(matches) = 0
        Debug "No matching message in INBOX."
        success = CkImap::ckDisconnect(imap)
        If success <> 1
            Debug CkImap::ckLastErrorText(imap)
            CkImap::ckDispose(imap)
            CkMessageSet::ckDispose(matches)
            ProcedureReturn
        EndIf

        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(matches)
        ProcedureReturn
    EndIf

    ;  The search returned UIDs. Use the same identifier type for the fetch.
    uid.i = CkMessageSet::ckGetId(matches,0)
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkImap::ckFetchEmail(imap,0,uid,1,email)
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(matches)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "Mailbox INBOX, UIDVALIDITY " + Str(CkImap::ckUidValidity(imap)) + ", UID " + Str(uid)
    success = CkImap::ckDisconnect(imap)
    If success <> 1
        Debug CkImap::ckLastErrorText(imap)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(matches)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    ;  The downloaded Email remains usable after disconnecting.
    Debug "Subject: " + CkEmail::ckSubject(email) + "  Files: " + Str(CkEmail::ckNumAttachments(email))

    success = CkEmail::ckSaveEml(email,"imap-received.eml")
    If success <> 1
        Debug CkEmail::ckLastErrorText(email)
        CkImap::ckDispose(imap)
        CkMessageSet::ckDispose(matches)
        CkEmail::ckDispose(email)
        ProcedureReturn
    EndIf

    Debug "Saved imap-received.eml."


    CkImap::ckDispose(imap)
    CkMessageSet::ckDispose(matches)
    CkEmail::ckDispose(email)


    ProcedureReturn
EndProcedure