Sample code for 30+ languages & platforms
VB.NET Requires Chilkat v11.0.0+

Gmail IMAP: Inspect Flags and Locate the Spam Folder

See more IMAP Examples

Inspect the current INBOX flags and discover Gmail's Spam folder through its \Junk special-use attribute. The example reads the first message's flags, if present, and makes no mailbox changes.

Client-specific Junk/NonJunk keywords are not a portable spam verdict. A mailbox's \Junk LIST attribute identifies a special folder; it is not a message flag. Use current server responses rather than a historical transcript. For Gmail label membership, see X-GM-LABELS.

First authorize the mailbox for https://mail.google.com/ and refresh the token as needed. Read the current token from gmail-mail-token.json in a private working directory. Narrow Gmail REST scopes do not authorize these protocols. Keep the token and authentication logs private.

See the Gmail SMTP, IMAP, and POP3 guide for current authentication choices and Gmail-specific examples.

Reference documentation

Chilkat VB.NET Downloads

VB.NET
Dim success As Boolean = False

'  Assumes Chilkat is unlocked. See Global Unlock Sample.

Dim token As New Chilkat.JsonObject
success = token.LoadFile("gmail-mail-token.json")
If (success <> True) Then
    Debug.WriteLine("Could not read gmail-mail-token.json from the private working directory.")
    Exit Sub
End If

If ((token.JsonTypeOf("access_token") <> 1) Or (token.StringOfEquals("access_token","",True) = True)) Then
    Debug.WriteLine("An OAuth access token is required. Authorize or refresh the account first.")
    Exit Sub
End If

Dim mailbox As String = "user@example.com"
Dim imap As New Chilkat.Imap
imap.Ssl = True
imap.Port = 993
imap.RequireSslCertVerify = True
imap.AuthMethod = "XOAUTH2"
success = imap.Connect("imap.gmail.com")
If (success <> True) Then
    Debug.WriteLine("Could not establish the Gmail IMAP TLS connection.")
    Exit Sub
End If

success = imap.Login(mailbox,token.StringOf("access_token"))
If (success <> True) Then
    imap.Disconnect()
    Debug.WriteLine("IMAP authentication failed. Check the mailbox, token scope, expiry, and Workspace policy.")
    Exit Sub
End If

success = imap.ExamineMailbox("INBOX")
If (success <> True) Then
    imap.Logout()
    imap.Disconnect()
    Debug.WriteLine("Could not examine INBOX.")
    Exit Sub
End If

'  Capture only the EXAMINE response, after authentication has completed.
Debug.WriteLine(imap.LastResponse)
'  Sample flags without marking messages read. Sequence numbers are session-local.
If (imap.NumMessages > 0) Then
    Dim sequence As UInt32 = 1
    Dim flags As String = imap.FetchFlags(sequence,False)
    If (imap.LastMethodSuccess <> True) Then
        imap.Logout()
        imap.Disconnect()
        Debug.WriteLine("Could not fetch flags; the message may have changed or disappeared.")
        Exit Sub
    End If

    Debug.WriteLine("Sequence " & sequence & ": " & flags)
End If

Dim folders As New Chilkat.Mailboxes
success = imap.MbxList(False,"","*",folders)
If (success <> True) Then
    imap.Logout()
    imap.Disconnect()
    Debug.WriteLine("Could not list folders.")
    Exit Sub
End If

Dim i As Integer = 0
While (i < folders.Count)
    If (folders.HasFlag(i,"\Junk") = True) Then
        Debug.WriteLine("Spam folder: " & folders.GetName(i))
    End If

    i = i + 1
End While
imap.Logout()
imap.Disconnect()