Sample code for 30+ languages & platforms
PowerBuilder

Mark IMAP Email as Read/Unread (Seen/Unseen)

Demonstrates how to mark emails as read or unread.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Imap
integer li_NumMsgs
oleobject loo_Email
integer i
integer li_BIsUid

li_Success = 0

// This example assumes 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("myLogin","myPassword")
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

// Set PeekMode so that downloaded messages are not
// automatically marked as seen.
loo_Imap.PeekMode = 1

// The NumMessages property contains the number of messages 
// in the currently selected mailbox.
li_NumMsgs = loo_Imap.NumMessages
if li_NumMsgs = 0 then
    destroy loo_Imap
    return
end if

loo_Email = create oleobject
li_rc = loo_Email.ConnectToNewObject("Chilkat.Email")

for i = 1 to li_NumMsgs
    // Download each email by sequence number (not UID)
    li_Success = loo_Imap.FetchEmail(0,i,0,loo_Email)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Email
        return
    end if

    // If desired, mark the email as SEEN.  There are two
    // ways to do it:

    // 1) Set the flag directly by using the sequence number
    // Indicate that we are passing a sequence number and
    // not a UID:
    li_BIsUid = 0
    // Set the SEEN flag = 1 to mark the email as SEEN,
    // or set it to 0 to mark it as not-seen.
    li_Success = loo_Imap.SetFlag(i,li_BIsUid,"SEEN",1)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Email
        return
    end if

    // 2) Alternatively, we can use the email object.
    // When an email is downloaded from the IMAP server
    // Chilkat will add a "ckx-imap-uid" header to the email.
    // This makes it possible to know the UID associated with
    // the email.  (This is not the sequence number, which may change
    // from session to session, but the UID which does not change.
    // The SetMailFlag method is identical to SetFlag, except
    // it gets the UID from the ckx-imap-uid header.
    // For example:
    li_Success = loo_Imap.SetMailFlag(loo_Email,"SEEN",1)
    if li_Success = 0 then
        Write-Debug loo_Imap.LastErrorText
        destroy loo_Imap
        destroy loo_Email
        return
    end if

next

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


destroy loo_Imap
destroy loo_Email