Sample code for 30+ languages & platforms
Classic ASP

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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

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

set imap = Server.CreateObject("Chilkat.Imap")

imap.Ssl = 1
imap.Port = 993

success = imap.Connect("imap.example.com")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

success = imap.Login("user@example.com","myPassword")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

success = imap.SelectMailbox("Inbox")
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

set msgSet = Server.CreateObject("Chilkat.MessageSet")
success = imap.QueryMbx("ALL",1,msgSet)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

set bundle = Server.CreateObject("Chilkat.EmailBundle")
success = imap.FetchMsgSet(1,msgSet,bundle)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If

set email = Server.CreateObject("Chilkat.Email")
If (bundle.MessageCount > 0) Then
    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 = 0) Then
        Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
        Response.End
    End If

    seen = imap.GetMailFlag(email,"Seen")
    Response.Write "<pre>" & Server.HTMLEncode( "Current Seen state: " & seen) & "</pre>"
End If

success = imap.Disconnect()
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( imap.LastErrorText) & "</pre>"
    Response.End
End If


%>
</body>
</html>