如何使用ImapX lib获取gmail的所有文件夹名称? C#

时间:2011-03-24 07:28:26

标签: c# email gmail imap imapx

如何使用ImapX lib获取gmail的所有文件夹名称? 我在http://hellowebapps.com/2010-02-09/imapx-net-library-to-manage-imap-folders/中读到但未找到所有文件夹部分。

3 个答案:

答案 0 :(得分:0)

以下是获取所有文件夹列表的方法......

        FolderCollection folders = client.GetFolders();
        foreach (Folder myfolder in folders)
        {
            MessageBox.Show(myfolder.Name);
        }

然后使用名称:

ImapX.MessageCollection messages = client.Folders["Spam"].Search("ALL", true);

请注意,文件夹名称区分大小写......

答案 1 :(得分:0)

您可以迭代SubFolder集合,并可以获取所有这些gamail文件夹及其路径。一个例子:

var client = new ImapClient(...);
client.Connection();
client.LogIn(...);


foreach (var item in WalkFolderTree(client.Folders))
{

     Console.WriteLine(item.FolderPath);
 }

client.LogOut();

您必须自定义实现遍历代码,如:

public IEnumerable<Folder> WalkFolderTree(FolderCollection folders)
{
    foreach (var item in folders)
    {
        if (item.HasChildren)
        {
            WalkFolderTree(item.SubFolder);
        }
            yield return item;
    }

 }

然后它将列出所有文件夹,如:

   INBOX
   ...
   [Gmail]
   [Gmail]/All Mail
   [Gmail]/Drafts
   [Gmail]/Sent Mail
   [Gmail]/Spam
   [Gmail]/Starred
   [Gmail]/Trash

答案 2 :(得分:0)

以下是:

        public List<string> getMailboxes(string emailAddress, string emailPassword)
    {
        var client = new ImapClient("imap.gmail.com", 993, true, true);
        if (client.Connect())
        {
            if (client.Login(emailAddress, emailPassword))
            {
                //get all parent folers
                var folders = client.Folders;

                foreach (var parentFolder in folders)
                {
                    //get parent folder path
                    var parentPath = parentFolder.Path;

                    //check if every parent folder has subfolder
                    if (parentFolder.HasChildren)
                    {
                        var subfolders = parentFolder.SubFolders;
                        foreach(var subfolder in subfolders)
                        {
                            var subPath = subfolder.Path;
                        }
                    }
                }
            }
        }
    }