Sample code for 30+ languages & platforms
Lianja

Refresh an Email Object's IMAP Flags

See more IMAP Examples

Demonstrates the Chilkat Imap.RefetchMailFlags method, which updates the flags stored on an Email object to the current values on the server. The only argument is the Email object. This example re-reads the flags for a previously fetched message and then checks its Seen state.

Background: Flags are shared mailbox state, so between the time you fetch a message and the time you act on it, another client (a phone, a webmail tab) may have marked it read, flagged, or deleted. RefetchMailFlags pulls just the flags — not the whole message — so a long-running job can cheaply re-check the live state before deciding what to do, avoiding acting on stale information.

Chilkat Lianja Downloads

Lianja
llSuccess = .F.

//  Demonstrates the Imap.RefetchMailFlags method, which updates the flags stored on an Email
//  object to the current values on the server.  The only argument is the Email object.

loImap = createobject("CkImap")

loImap.Ssl = .T.
loImap.Port = 993

llSuccess = loImap.Connect("imap.example.com")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.Login("user@example.com","myPassword")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

llSuccess = loImap.SelectMailbox("Inbox")
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    return
endif

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

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

loEmail = createobject("CkEmail")
if (loBundle.MessageCount > 0) then
    llSuccess = loBundle.EmailAt(0,loEmail)

    //  Re-read the flags from the server, in case another client changed them since the fetch.
    llSuccess = loImap.RefetchMailFlags(loEmail)
    if (llSuccess = .F.) then
        ? loImap.LastErrorText
        release loImap
        release loMsgSet
        release loBundle
        release loEmail
        return
    endif

    lnSeen = loImap.GetMailFlag(loEmail,"Seen")
    ? "Current Seen state: " + str(lnSeen)
endif

llSuccess = loImap.Disconnect()
if (llSuccess = .F.) then
    ? loImap.LastErrorText
    release loImap
    release loMsgSet
    release loBundle
    release loEmail
    return
endif



release loImap
release loMsgSet
release loBundle
release loEmail