Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loImap
LOCAL lnFetchUids
LOCAL loMessageSet
LOCAL loBundle
LOCAL lnHeadersOnly
LOCAL loEmail
LOCAL loMsgSet1
LOCAL i
LOCAL lnSzBundle
LOCAL lcUidStr

lnSuccess = 0

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

loImap = CreateObject('Chilkat.Imap')

* Connect to an IMAP server.
* Use TLS
loImap.Ssl = 1
loImap.Port = 993
lnSuccess = loImap.Connect("imap.example.com")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* Login
lnSuccess = loImap.Login("login","password")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

* Select an IMAP mailbox
lnSuccess = loImap.SelectMailbox("Inbox")
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    CANCEL
ENDIF

lnFetchUids = 1
* Get the message UIDs of all the emails in the mailbox

loMessageSet = CreateObject('Chilkat.MessageSet')
lnSuccess = loImap.QueryMbx("ALL",lnFetchUids,loMessageSet)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMessageSet
    CANCEL
ENDIF

loBundle = CreateObject('Chilkat.EmailBundle')
lnHeadersOnly = 1
lnSuccess = loImap.FetchMsgSet(lnHeadersOnly,loMessageSet,loBundle)
IF (lnSuccess = 0) THEN
    ? loImap.LastErrorText
    RELEASE loImap
    RELEASE loMessageSet
    RELEASE loBundle
    CANCEL
ENDIF

* The UID of each fetched email header is available 
* in the ckx-imap-uid header field.
loEmail = CreateObject('Chilkat.Email')
loMsgSet1 = CreateObject('Chilkat.MessageSet')
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.
    lnSuccess = loImap.MoveMessages(loMsgSet1,"someOtherFolder")
    IF (lnSuccess <> 1) THEN
        ? loImap.LastErrorText
        * Exit the loop...
        i = lnSzBundle
    ENDIF

    i = i + 1
ENDDO

* Disconnect from the IMAP server.
lnSuccess = loImap.Disconnect()

RELEASE loImap
RELEASE loMessageSet
RELEASE loBundle
RELEASE loEmail
RELEASE loMsgSet1