Sample code for 30+ languages & platforms
Tcl

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

Tcl

load ./chilkat.dll

set success 0

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

set imap [new_CkImap]

# Connect to an IMAP server.
# Use TLS
CkImap_put_Ssl $imap 1
CkImap_put_Port $imap 993
set success [CkImap_Connect $imap "imap.example.com"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

# Login
set success [CkImap_Login $imap "login" "password"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

# Select an IMAP mailbox
set success [CkImap_SelectMailbox $imap "Inbox"]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    exit
}

set fetchUids 1
# Get the message UIDs of all the emails in the mailbox

set messageSet [new_CkMessageSet]

set success [CkImap_QueryMbx $imap "ALL" $fetchUids $messageSet]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $messageSet
    exit
}

set bundle [new_CkEmailBundle]

set headersOnly 1
set success [CkImap_FetchMsgSet $imap $headersOnly $messageSet $bundle]
if {$success == 0} then {
    puts [CkImap_lastErrorText $imap]
    delete_CkImap $imap
    delete_CkMessageSet $messageSet
    delete_CkEmailBundle $bundle
    exit
}

# The UID of each fetched email header is available 
# in the ckx-imap-uid header field.
set email [new_CkEmail]

set msgSet1 [new_CkMessageSet]

set i 0
set szBundle [CkEmailBundle_get_MessageCount $bundle]
while {$i < $szBundle} {
    CkEmailBundle_EmailAt $bundle $i $email

    # Build a message set containing one UID
    set uidStr [CkEmail_getHeaderField $email "ckx-imap-uid"]
    CkMessageSet_FromCompactString $msgSet1 $uidStr

    # Move this message to some other folder.
    set success [CkImap_MoveMessages $imap $msgSet1 "someOtherFolder"]
    if {$success != 1} then {
        puts [CkImap_lastErrorText $imap]
        # Exit the loop...
        set i $szBundle
    }

    set i [expr $i + 1]
}

# Disconnect from the IMAP server.
set success [CkImap_Disconnect $imap]

delete_CkImap $imap
delete_CkMessageSet $messageSet
delete_CkEmailBundle $bundle
delete_CkEmail $email
delete_CkMessageSet $msgSet1