Sample code for 30+ languages & platforms
PureBasic

Get IMAP UID from Email Header

See more IMAP Examples

Demonstrates how to get the IMAP UID from an email header. After fetching an email or header using IMAP, the UID is contained in the ckx-imap-uid header field.

Chilkat PureBasic Downloads

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

Procedure ChilkatExample()

    success.i = 0

    ; This example requires 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,"login","password")
    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

    fetchUids.i = 1
    ; Get the message UIDs of all the emails in the mailbox

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

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

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

    headersOnly.i = 1
    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

    ; The UID of each fetched email header is available 
    ; in the ckx-imap-uid header field.
    email.i = CkEmail::ckCreate()
    If email.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

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

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

        ; Build a message set containing one UID
        uidStr.s = CkEmail::ckGetHeaderField(email,"ckx-imap-uid")
        CkMessageSet::ckFromCompactString(msgSet1,uidStr)

        ; Move this message to some other folder.
        success = CkImap::ckMoveMessages(imap,msgSet1,"someOtherFolder")
        If success <> 1
            Debug CkImap::ckLastErrorText(imap)
            ; Exit the loop...
            i = szBundle
        EndIf

        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)
    CkMessageSet::ckDispose(msgSet1)


    ProcedureReturn
EndProcedure