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

Create an IMAP Folder: Gmail Labels with OAuth2

Create a custom Gmail label and a nested label using CreateMailbox. The example lists existing mailboxes first, skips names already present, and uses the hierarchy separator returned by LIST.

This creates labels in the account. A top-level name is not automatically a child of INBOX. In Gmail, IMAP mailboxes represent labels; creating a label does not apply it to a message. Chilkat handles Unicode mailbox names in its mailbox methods. If creating the child fails, the parent remains. A concurrent creation can require listing again.

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

'  This creates a custom Gmail label and one nested label.
Dim parent As String = "Example"
Dim folders As New Chilkat.Mailboxes
success = imap.MbxList(False,"","*",folders)
If (success <> True) Then
    imap.Logout()
    imap.Disconnect()
    Debug.WriteLine("Could not list existing labels.")
    Exit Sub
End If

Dim separator As String = imap.SeparatorChar
If (separator = "") Then
    imap.Logout()
    imap.Disconnect()
    Debug.WriteLine("The server did not provide a hierarchy separator.")
    Exit Sub
End If

If (folders.GetMailboxIndex(parent) < 0) Then
    success = imap.CreateMailbox(parent)
    If (success <> True) Then
        imap.Logout()
        imap.Disconnect()
        Debug.WriteLine("Could not create the parent label.")
        Exit Sub
    End If

End If

Dim child As New Chilkat.StringBuilder
child.Append(parent)
child.Append(separator)
child.Append("Child")
If (folders.GetMailboxIndex(child.GetAsString()) < 0) Then
    success = imap.CreateMailbox(child.GetAsString())
    If (success <> True) Then
        imap.Logout()
        imap.Disconnect()
        Debug.WriteLine("Could not create the child label. The parent may already exist.")
        Exit Sub
    End If

End If

Debug.WriteLine("Labels available: " & parent & " and " & child.GetAsString())
imap.Logout()
imap.Disconnect()