Sample code for 30+ languages & platforms
Lianja

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 Lianja Downloads

Lianja
llSuccess = .F.

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

loImap = createobject("CkImap")

// Connect to an IMAP server.
// Use TLS
loImap.Ssl = .T.
loImap.Port = 993
llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Login
llSuccess = loImap.Login("login","password")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

// Select an IMAP mailbox
llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llFetchUids = .T.
// Get the message UIDs of all the emails in the mailbox

loMessageSet = createobject("CkMessageSet")
llSuccess = loImap.QueryMbx("ALL",llFetchUids,loMessageSet)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    return
endif

loBundle = createobject("CkEmailBundle")
llHeadersOnly = .T.
llSuccess = loImap.FetchMsgSet(llHeadersOnly,loMessageSet,loBundle)
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMessageSet
    release loBundle
    return
endif

// The UID of each fetched email header is available 
// in the ckx-imap-uid header field.
loEmail = createobject("CkEmail")
loMsgSet1 = createobject("CkMessageSet")
i = 0
lnSzBundle = loBundle.MessageCount
do while i < lnSzBundle
    loBundle.EmailAt(i,loEmail)

    // Build a message set containing one UID
    lcUidStr = loEmail.GetHeaderField("ckx-imap-uid")
    loMsgSet1.FromCompactString(lcUidStr)

    // Move this message to some other folder.
    llSuccess = loImap.MoveMessages(loMsgSet1,"someOtherFolder")
    if (llSuccess <> .T.) then
        ? loImap.LastErrorText
        // Exit the loop...
        i = lnSzBundle
    endif

    i = i + 1
enddo

// Disconnect from the IMAP server.
llSuccess = loImap.Disconnect()


release loImap
release loMessageSet
release loBundle
release loEmail
release loMsgSet1