Sample code for 30+ languages & platforms
PowerBuilder

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

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_FetchUids
oleobject loo_MessageSet
oleobject loo_Bundle
integer li_HeadersOnly
oleobject loo_Email
oleobject loo_MsgSet1
integer i
integer li_SzBundle
string ls_UidStr

li_Success = 0

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

loo_Imap = create oleobject
li_rc = loo_Imap.ConnectToNewObject("Chilkat.Imap")
if li_rc < 0 then
    destroy loo_Imap
    MessageBox("Error","Connecting to COM object failed")
    return
end if

// Connect to an IMAP server.
// Use TLS
loo_Imap.Ssl = 1
loo_Imap.Port = 993
li_Success = loo_Imap.Connect("imap.example.com")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Login
li_Success = loo_Imap.Login("login","password")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

// Select an IMAP mailbox
li_Success = loo_Imap.SelectMailbox("Inbox")
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    return
end if

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

loo_MessageSet = create oleobject
li_rc = loo_MessageSet.ConnectToNewObject("Chilkat.MessageSet")

li_Success = loo_Imap.QueryMbx("ALL",li_FetchUids,loo_MessageSet)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MessageSet
    return
end if

loo_Bundle = create oleobject
li_rc = loo_Bundle.ConnectToNewObject("Chilkat.EmailBundle")

li_HeadersOnly = 1
li_Success = loo_Imap.FetchMsgSet(li_HeadersOnly,loo_MessageSet,loo_Bundle)
if li_Success = 0 then
    Write-Debug loo_Imap.LastErrorText
    destroy loo_Imap
    destroy loo_MessageSet
    destroy loo_Bundle
    return
end if

// The UID of each fetched email header is available 
// in the ckx-imap-uid header field.
loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

loo_MsgSet1 = create oleobject
li_rc = loo_MsgSet1.ConnectToNewObject("Chilkat.MessageSet")

i = 0
li_SzBundle = loo_Bundle.MessageCount
do while i < li_SzBundle
    loo_Bundle.EmailAt(i,loo_Email)

    // Build a message set containing one UID
    ls_UidStr = loo_Email.GetHeaderField("ckx-imap-uid")
    loo_MsgSet1.FromCompactString(ls_UidStr)

    // Move this message to some other folder.
    li_Success = loo_Imap.MoveMessages(loo_MsgSet1,"someOtherFolder")
    if li_Success <> 1 then
        Write-Debug loo_Imap.LastErrorText
        // Exit the loop...
        i = li_SzBundle
    end if

    i = i + 1
loop

// Disconnect from the IMAP server.
li_Success = loo_Imap.Disconnect()


destroy loo_Imap
destroy loo_MessageSet
destroy loo_Bundle
destroy loo_Email
destroy loo_MsgSet1