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

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

Xbase++
LOCAL nSuccess
LOCAL oImap
LOCAL nFetchUids
LOCAL oMessageSet
LOCAL oBundle
LOCAL nHeadersOnly
LOCAL oEmail
LOCAL oMsgSet1
LOCAL i
LOCAL nSzBundle
LOCAL cUidStr

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("login", "password")
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

nFetchUids := 1
//  Get the message UIDs of all the emails in the mailbox

oMessageSet := CreateObject("Chilkat.MessageSet")
nSuccess := oImap:QueryMbx("ALL", nFetchUids, oMessageSet)
IF (nSuccess == 0)
    ? oImap:LastErrorText
    oImap:destroy()
    oMessageSet:destroy()
    RETURN
ENDIF

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

//  The UID of each fetched email header is available 
//  in the ckx-imap-uid header field.
oEmail := CreateObject("Chilkat.Email")
oMsgSet1 := CreateObject("Chilkat.MessageSet")
i := 0
nSzBundle := oBundle:MessageCount
DO WHILE i < nSzBundle
    oBundle:EmailAt(i, oEmail)

    //  Build a message set containing one UID
    cUidStr := oEmail:GetHeaderField("ckx-imap-uid")
    oMsgSet1:FromCompactString(cUidStr)

    //  Move this message to some other folder.
    nSuccess := oImap:MoveMessages(oMsgSet1, "someOtherFolder")
    IF (nSuccess != 1)
        ? oImap:LastErrorText
        //  Exit the loop...
        i := nSzBundle
    ENDIF

    i := i + 1
ENDDO

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

oImap:destroy()
oMessageSet:destroy()
oBundle:destroy()
oEmail:destroy()
oMsgSet1:destroy()