Sample code for 30+ languages & platforms
Ruby

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

Ruby
require 'chilkat'

success = false

#  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.

imap = Chilkat::CkImap.new()

imap.put_Ssl(true)
imap.put_Port(993)

success = imap.Connect("imap.example.com")
if (success == false)
    print imap.lastErrorText() + "\n";
    exit
end

success = imap.Login("user@example.com","myPassword")
if (success == false)
    print imap.lastErrorText() + "\n";
    exit
end

success = imap.SelectMailbox("Inbox")
if (success == false)
    print imap.lastErrorText() + "\n";
    exit
end

msgSet = Chilkat::CkMessageSet.new()
success = imap.QueryMbx("ALL",true,msgSet)
if (success == false)
    print imap.lastErrorText() + "\n";
    exit
end

bundle = Chilkat::CkEmailBundle.new()
success = imap.FetchMsgSet(true,msgSet,bundle)
if (success == false)
    print imap.lastErrorText() + "\n";
    exit
end

email = Chilkat::CkEmail.new()
if (bundle.get_MessageCount() > 0)
    success = bundle.EmailAt(0,email)

    #  Re-read the flags from the server, in case another client changed them since the fetch.
    success = imap.RefetchMailFlags(email)
    if (success == false)
        print imap.lastErrorText() + "\n";
        exit
    end

    seen = imap.GetMailFlag(email,"Seen")
    print "Current Seen state: " + seen.to_s() + "\n";
end

success = imap.Disconnect()
if (success == false)
    print imap.lastErrorText() + "\n";
    exit
end