Zip Component, Email Component, Encryption Component ActiveX Control for Zip Compression .NET Components for ASP.NET
ActiveX and .NET Components for Zip Compression, Encryption, Email, XML, S/MIME, HTML Email, Character Encoding, Digital Certificates, FTP, and more ASP Email ActiveX Component


Index of Chilkat Blog Posts

February 28, 2008

C# to Display IMAP Folders in TreeView

This C# sample code shows how to use the Chilkat IMAP component to display mailbox folders in a System.Windows.Forms.TreeView class.

protected TreeNode findTreeNodeChild(TreeNodeCollection tnc, string key)
{
    int i;
    for (i = 0; i < tnc.Count; i++)
    {
        if (tnc[i].Text.Equals(key))
        {
            return tnc[i];
        }
    }
    return null;
}
	
protected void refreshFolderTreeView(Chilkat.Imap imap)
{
    // Clear the treeview
    treeView1.Nodes.Clear();
	
    // The ListMailboxes method returns a Mailboxes object
    // that contains the collection of mailboxes.
    // It accepts two arguments: a refName and a wildcardedMailbox.
	
    string refName;
    refName = "";
    // refName is usually set to an empty string.
    // A non-empty reference name argument is the name of a mailbox or a level of
    // mailbox hierarchy, and indicates the context in which the mailbox
    // name is interpreted.
	
    // Select all mailboxes matching this pattern:
    string wildcardedMailbox;
    wildcardedMailbox = "*";
	
    Chilkat.Mailboxes mboxes;
    mboxes = imap.ListMailboxes(refName, wildcardedMailbox);
    textBox1.Text = imap.LastErrorText;
	
    if (mboxes == null)
    {
        textBox1.Text = imap.LastErrorText;
        return;
    }
	
    // Get the mailbox hierarchy separator char:
    char sepChar = (char)imap.SeparatorChar;
	
    int i;
    for (i = 0; i <= mboxes.Count - 1; i++)
    {
        // The mailbox name will be something like:
        // Inbox.Trash, Inbox.Sent, or Inbox.myVendors.Acme
        // Split the mailbox name using the separator char and
        // add to the treeview.
        string[] folders = mboxes.GetName(i).Split(sepChar);
	
        int j;
        TreeNodeCollection tnc = treeView1.Nodes;
        for (j = 0; j < folders.Length; j++)
        {
            TreeNode tn = findTreeNodeChild(tnc, folders[j]);
            if (tn == null)
            {
                // Add the node.
                tn = new TreeNode(folders[j]);
                tnc.Add(tn);
            }
            tnc = tn.Nodes;
        }
    }
	
}
	
private void btnShowFolders_Click(object sender, EventArgs e)
{
    Chilkat.Imap imap = new Chilkat.Imap();
	
    // Any string passed to UnlockComponent starts a fully-functional 30-day trial
    bool success = imap.UnlockComponent("Anything for 30-day trial");
    if (!success)
    {
        MessageBox.Show(imap.LastErrorText);
        return;
    }
	
    success = imap.Connect("mail.chilkatsoft.com");
    if (!success)
    {
        MessageBox.Show(imap.LastErrorText);
        return;
    }
	
    // Login
    if (!imap.Login("matt@chilkatsoft.com", "****"))
    {
        MessageBox.Show(imap.LastErrorText);
        return;
    }
	
    refreshFolderTreeView(imap);
	
}


Privacy Statement. Copyright 2000-2011 Chilkat Software, Inc. All rights reserved.
Send feedback to support@chilkatsoft.com

Components for Microsoft Windows XP, 2000, 2003 Server, Vista, Windows 7, and Windows 95/98/NT4.