Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ImapResponseException重复文件夹收件箱_C#_Visual Studio_Email_Exchange Server_Imap - Fatal编程技术网

C# ImapResponseException重复文件夹收件箱

C# ImapResponseException重复文件夹收件箱,c#,visual-studio,email,exchange-server,imap,C#,Visual Studio,Email,Exchange Server,Imap,我正在使用limilabs开发一个c#应用程序。使用以下代码,我正在从服务器检索邮件 try { imap.ServerCertificateValidate += new ServerCertificateValidateEventHandler(Validate); imap.Connect(server); Console.WriteLine("Imap Con

我正在使用limilabs开发一个c#应用程序。使用以下代码,我正在从服务器检索邮件

     try
            {
                imap.ServerCertificateValidate += new ServerCertificateValidateEventHandler(Validate);
                imap.Connect(server);
                Console.WriteLine("Imap Connected");
                imap.UseBestLogin(username, password);
                Console.WriteLine("Imap Logged in");
                string[] folders = new string[] { "Verwijderde items", "INBOX"};
                foreach (string folder in folders)
                {
                    if (folder.Equals(string.Empty))
                        break;

                    foreach (FolderInfo folderInfo in imap.GetFolders())
                    {
                        if (folderInfo.Name.Equals(folder))
                        {
                            imap.Select(folder); //ImapResponseException

                            //Process found mail

                            break;
                        }
                    }                        
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Imap not Connected");
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.StackTrace);
            }
“verwijderde items”
是荷兰语,表示删除的项目或回收站

ImapResponseException:在邮箱中检测到重复的文件夹收件箱。因此,用户的连接已断开。

抛出此异常是因为我用于登录的用户名和密码可以访问所有员工的收件箱。也许我需要使用
imap.Select(“mail@adres.com/收件箱/+文件夹“
,指定要访问的用途。但我不知道imap使用的文件结构。也许这不是这个特定dll的解决方案。欢迎提出任何意见

我要连接的服务器是使用Microsoft Exchange 2007的Windows server 2008 R2

更新1:
Mail.dll文档:

问题可能是您正在调用
imap.Select(文件夹)
当您可能应该调用
folderInfo
上的
Select()
方法,或者调用
folderInfo.Path
字符串时(我不确定
folderInfo
有哪些属性)

如果您愿意切换到免费/开源IMAP库,我建议使用我的库

要执行您在MailKit中尝试执行的操作,代码如下所示:

try {
    using (var client = new ImapClient ()) {
        client.Connect (server, 993, true); // I assume you are connecting via SSL
        client.Authenticate (username, password);

        string[] names = new string[] { "Verwijderde items", "INBOX" };

        // client.PersonalNamespaces are the mailboxes that belong to you
        foreach (var personalNamespace in client.PersonalNamespaces) {
            // get the root folder in your personal namespace
            // (there's always at least 1 personal namespace)
            var root = client.GetFolder (personalNamespace);

            // get the top-level folders in your personal namespace
            foreach (var folder in root.GetSubfolders ()) {
                foreach (var name in names) {
                    if (folder.Name == name) {
                        // Select the folder (if you want to change flags,
                        // use FolderAccess.ReadWrite)
                        folder.Open (FolderAccess.ReadOnly);

                        // process mail
                    }
                }
            }
        }

        // client.SharedNamespaces are the shared mailboxes
        foreach (var sharedNamespace in client.SharedNamespaces) {
            var root = client.GetFolder (sharedNamespace);

            // get the top-level folders in your shared namespace
            foreach (var folder in root.GetSubfolders ()) {
                foreach (var name in names) {
                    if (folder.Name == name) {
                        // Select the folder (if you want to change flags,
                        // use FolderAccess.ReadWrite)
                        folder.Open (FolderAccess.ReadOnly);

                        // process mail
                    }
                }
            }
        }
    }
} catch (Exception ex) {
    Console.WriteLine ("Imap not Connected");
    Console.WriteLine (ex.Message);
    Console.WriteLine (ex.StackTrace);
}
void ProcessMail (ImapClient client, FolderNamespaceCollection namespaces)
{
    string[] names = new string[] { "Verwijderde items", "INBOX" };

    // client.PersonalNamespaces are the mailboxes that belong to you
    foreach (var @namespace in namespaces) {
        // get the root folder in the namespace
        var root = client.GetFolder (@namespace);

        // get the top-level folders in the namespace
        foreach (var folder in root.GetSubfolders ()) {
            foreach (var name in names) {
                if (folder.Name == name) {
                    // Select the folder (if you want to change flags,
                    // use FolderAccess.ReadWrite)
                    folder.Open (FolderAccess.ReadOnly);

                    // process mail
                }
            }
        }
    }
}
显然,您可以简化上述代码,因为您可以编写一个方法,该方法采用
FolderNamespaceCollection
参数,只需将个人或共享名称空间传递给它,如下所示:

try {
    using (var client = new ImapClient ()) {
        client.Connect (server, 993, true); // I assume you are connecting via SSL
        client.Authenticate (username, password);

        string[] names = new string[] { "Verwijderde items", "INBOX" };

        // client.PersonalNamespaces are the mailboxes that belong to you
        foreach (var personalNamespace in client.PersonalNamespaces) {
            // get the root folder in your personal namespace
            // (there's always at least 1 personal namespace)
            var root = client.GetFolder (personalNamespace);

            // get the top-level folders in your personal namespace
            foreach (var folder in root.GetSubfolders ()) {
                foreach (var name in names) {
                    if (folder.Name == name) {
                        // Select the folder (if you want to change flags,
                        // use FolderAccess.ReadWrite)
                        folder.Open (FolderAccess.ReadOnly);

                        // process mail
                    }
                }
            }
        }

        // client.SharedNamespaces are the shared mailboxes
        foreach (var sharedNamespace in client.SharedNamespaces) {
            var root = client.GetFolder (sharedNamespace);

            // get the top-level folders in your shared namespace
            foreach (var folder in root.GetSubfolders ()) {
                foreach (var name in names) {
                    if (folder.Name == name) {
                        // Select the folder (if you want to change flags,
                        // use FolderAccess.ReadWrite)
                        folder.Open (FolderAccess.ReadOnly);

                        // process mail
                    }
                }
            }
        }
    }
} catch (Exception ex) {
    Console.WriteLine ("Imap not Connected");
    Console.WriteLine (ex.Message);
    Console.WriteLine (ex.StackTrace);
}
void ProcessMail (ImapClient client, FolderNamespaceCollection namespaces)
{
    string[] names = new string[] { "Verwijderde items", "INBOX" };

    // client.PersonalNamespaces are the mailboxes that belong to you
    foreach (var @namespace in namespaces) {
        // get the root folder in the namespace
        var root = client.GetFolder (@namespace);

        // get the top-level folders in the namespace
        foreach (var folder in root.GetSubfolders ()) {
            foreach (var name in names) {
                if (folder.Name == name) {
                    // Select the folder (if you want to change flags,
                    // use FolderAccess.ReadWrite)
                    folder.Open (FolderAccess.ReadOnly);

                    // process mail
                }
            }
        }
    }
}
但是,更简单的方法是这样,因为您已经知道所需的文件夹名称:

void ProcessMail (ImapClient client, FolderNamespaceCollection namespaces)
{
    string[] names = new string[] { "Verwijderde items", "INBOX" };

    foreach (var @namespace in namespaces) {
        // get the root folder in the namespace
        var root = client.GetFolder (@namespace);

        // get specific folders:
        foreach (var name in names) {
            try {
                var folder = root.GetSubfolder (name);

                // Select the folder (if you want to change flags,
                // use FolderAccess.ReadWrite)
                folder.Open (FolderAccess.ReadOnly);

                // process mail
            } catch (FolderNotFoundException) {
            }
        }
    }
}

确实存在Imap.select(folderinfo),但给出了相同的异常。文档可以在这里找到:看起来我关于folderInfo.Name代表的内容的假设是错误的。我猜这是基于这些文档的完整路径,但我假设它是邮箱名,没有文件夹路径。我已经更新了我的答案,以显示如果您感兴趣,如何使用我自己的IMAP库来做您想做的事情。