我正在使用来自limilabs的mail.dll来处理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: Duplicate folder INBOX were detected in the mailbox. Therefore the user's connection was disconnected.
抛出此异常是因为我用于登录的用户名和密码可以访问所有员工的收件箱。也许我需要使用imap.Select("mail@adres.com/inbox/ + folder")
来指定我想要访问的用途。但我不知道imap使用的文件结构。也许这不是这个特定dll的解决方案。欢迎所有的想法。
我连接的服务器是带有Microsoft Exchange 2007的Windows Server 2008 R2。
更新1: Mail.dll文档:http://www.limilabs.com/static/mail/documentation/
答案 0 :(得分:0)
问题很可能是你正在调用imap.Select(folder);
当你应该做的是调用Select()
上的folderInfo
方法或folderInfo.Path
字符串时({I}}我不确定folderInfo
有哪些属性。
如果您愿意切换到免费/开源IMAP库,我建议您使用我的MailKit库。
要在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);
}
显然,您可以简化上面的代码,因为您可以编写一个带有FolderNamespaceCollection
参数的方法,并将其传递给个人或共享名称空间,如下所示:
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) {
}
}
}
}