MailSystem.Net删除消息,IndexOnServer属性= 0

我正在使用MailSystem.NET并尝试从服务器删除消息。 问题是IndexOnServer属性为0,我收到以下错误:

Command "store 0 +flags.silent (\Deleted)" failed : 121031084812790 BAD Error in IMAP command STORE: Invalid messageset 

这是我的代码:

  Imap4Client client = new Imap4Client(); client.Connect(account.Server, account.Username, account.Password); var inbox = client.SelectMailbox("Inbox"); MessageCollection messages = inbox.SearchParse("SINCE " + DateTime.Now.AddHours(-hours).ToString("dd-MMM-yyyy")); foreach (Message newMessage in messages) { inbox.DeleteMessage(newMessage.IndexOnServer, true); } 

如何获取正确的邮件索引以便删除?


编辑:

使用标准的基于1的循环的建议的问题是计数器索引将不与消息索引同步,因为在我的情况下我正在搜索仅检索特定的消息子集(据我所知) 。

谢谢。

您可以尝试通过UID进行删除,这对每条消息都应该更加可靠和唯一。 这对我来说过去很有用。

编辑:由于删除邮件会导致所有索引向下移动一个,因此您可以使用两个单独的计数器。 一个用于跟踪迭代整个盒子的时间(messagesLeft),另一个用于跟踪当前消息索引,如果消息被删除,它将减少1(因为它会向上移动一个位置)。

 Mailbox box = client.AllMailboxes["inbox"]; Fetch fetch = box.Fetch; int messagesLeft = box.Count; int msgIndex = 0; while (messagesLeft > 0) { msgIndex++; messagesLeft--; Message email = fetch.MessageObject(msgIndex); if (criteria) { box.UidDeleteMessage(fetch.Uid(msgIndex), true); msgIndex--; } } 

在回复您的评论时,这里有一个更全面的例子,说明如何在不关心数字位置/索引的情况下使用UID进行删除。

 class Email { int UID { get; set; } DateTime Sent { get; set; } public string Body { get; set; } // put whichever properties you will need } List GetEmails(string mailbox); { Mailbox box = client.AllMailboxes[mailbox]; Fetch fetch = box.Fetch; List list = new List(); for (int x = 1; x <= box.MessageCount; x++) { Message msg = fetch.MessageObject(x); list.Add(new Email() { } // set properties from the msg object } return list; } void DeleteEmail(Email email, string mailbox) { Mailbox box = client.AllMailboxes[mailbox]; box.UidDeleteMessage(email.Uid, true); } static void Main() { List emails = GetEmails("inbox"); emails = emails.Where(email => email.Sent < DateTime.Now.AddHours(-hours)) foreach (Email email in emails) DeleteEmail(email); } 

这是文档化的官方示例。 而不是inbox.search(“all”)将其更改为您的搜索查询等。

http://mailsystem.codeplex.com/discussions/269304

  //action_id is the Message.MessageId of the email //action_flag is the Gmail special folder to move to (Trash) //copying to Trash removes the email from the inbox, but it can't be moved back once done, even from the web interface public static void move_msg_to(string action_id, string action_flag) { Imap4Client imap = new Imap4Client(); imap.ConnectSsl("imap.gmail.com", 993); imap.Login("heythatsme@gmail.com", "heythatsmypassword"); imap.Command("capability"); Mailbox inbox = imap.SelectMailbox("inbox"); int[] ids = inbox.Search("ALL"); if (ids.Length > 0) { Message msg = null; for (var i = 0; i < ids.Length; i++) { msg = inbox.Fetch.MessageObject(ids[i]); if (msg.MessageId == action_id) { imap.Command("copy " + ids[i].ToString() + " [Gmail]/" + action_flag); break; } } } } 

只需在消息索引中添加1即可。

  foreach (Message newMessage in messages) { inbox.DeleteMessage(newMessage.IndexOnServer + 1, true); } 

尝试先删除最后一条消息然后再删除最后一条消息,然后再删除第一条消

如果您删除并删除已设置,则消息编号会更改。