Sample code for 30+ languages & platforms
Go Requires Chilkat v11.1.0+

Gmail IMAP: Discover Special Folders

See more GMail SMTP/IMAP/POP Examples

Find Gmail special folders from the attributes returned by LIST, using MbxList and Mailboxes.HasFlag. The result maps roles such as Sent and Trash to the server-provided mailbox names. Chilkat decodes mailbox names for use with its mailbox methods.

Folder names can be localized and visibility can differ by account. The role attributes are stable; do not hard-code [Gmail]/Sent Mail or use the deprecated XLIST command. INBOX is a protocol-defined name. Missing map keys indicate folders not advertised by the server.

Use the mailbox that authorized the full-mail scope https://mail.google.com/. Obtain the protocol OAuth token and refresh it before running this example. The token is loaded from gmail-mail-token.json in a private working directory; a narrow Gmail REST token is insufficient.

Google documentation

Chilkat Go Downloads

Go
    success := false

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.

    token := chilkat.NewJsonObject()
    success = token.LoadFile("gmail-mail-token.json")
    if success != true {
        fmt.Println("Could not load the protocol token from the private working directory.")
        token.DisposeJsonObject()
        return
    }

    if (token.JsonTypeOf("access_token") != 1) || (token.StringOfEquals("access_token","",true) == true) {
        fmt.Println("Authorize or refresh the mailbox first.")
        token.DisposeJsonObject()
        return
    }

    mailbox := "user@example.com"
    imap := chilkat.NewImap()
    imap.SetSsl(true)
    imap.SetPort(993)
    imap.SetRequireSslCertVerify(true)
    imap.SetAuthMethod("XOAUTH2")
    success = imap.Connect("imap.gmail.com")
    if success != true {
        fmt.Println("Could not establish the Gmail IMAP TLS connection.")
        token.DisposeJsonObject()
        imap.DisposeImap()
        return
    }

    success = imap.Login(mailbox,*token.StringOf("access_token"))
    if success != true {
        imap.Disconnect()
        fmt.Println("IMAP authentication failed. Check the mailbox, scope, expiry, and account policy.")
        token.DisposeJsonObject()
        imap.DisposeImap()
        return
    }

    folders := chilkat.NewMailboxes()
    success = imap.MbxList(false,"","*",folders)
    if success != true {
        imap.Logout()
        imap.Disconnect()
        fmt.Println("Could not list Gmail folders.")
        token.DisposeJsonObject()
        imap.DisposeImap()
        folders.DisposeMailboxes()
        return
    }

    //  LIST special-use attributes work with localized folder names. XLIST is obsolete.
    special := chilkat.NewJsonObject()
    i := 0
    for (i < folders.Count()) {
        if folders.IsSelectable(i) == true {
            fmt.Println(*folders.GetName(i), "  ", *folders.GetFlags(i))
            if folders.HasFlag(i,"\\All") == true {
                special.UpdateString("allMail",*folders.GetName(i))
            }

            if folders.HasFlag(i,"\\Sent") == true {
                special.UpdateString("sent",*folders.GetName(i))
            }

            if folders.HasFlag(i,"\\Drafts") == true {
                special.UpdateString("drafts",*folders.GetName(i))
            }

            if folders.HasFlag(i,"\\Junk") == true {
                special.UpdateString("spam",*folders.GetName(i))
            }

            if folders.HasFlag(i,"\\Trash") == true {
                special.UpdateString("trash",*folders.GetName(i))
            }

            if folders.HasFlag(i,"\\Flagged") == true {
                special.UpdateString("starred",*folders.GetName(i))
            }

            if folders.HasFlag(i,"\\Important") == true {
                special.UpdateString("important",*folders.GetName(i))
            }

        }

        i = i + 1
    }

    imap.Logout()
    imap.Disconnect()
    special.SetEmitCompact(false)
    fmt.Println(*special.Emit())
    //  Missing keys mean the folder was not advertised. Do not substitute an English name.

    token.DisposeJsonObject()
    imap.DisposeImap()
    folders.DisposeMailboxes()
    special.DisposeJsonObject()