MailSystem.Net Delete the message, property IndexOnServer = 0

I am using MailSystem.NET and trying to delete a message from the server. The problem is that the IndexOnServer property is 0, and I get the following error:

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

Here is my code:

  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); } 

How do I get the correct post index so that it can be deleted?


Edit:

The problem with sentences using a standard 1-based loop is that the counter index will not synchronize with the message index, since in my case I am only looking to get a specific subset of messages (since I understand this).

Thanks.

+6
source share
4 answers

You can try to remove the UID , which should be more reliable and unique for each message. It worked well for me in the past.

Edit:. When deleting a message, all indexes are shifted by one, you can use two separate counters. One to keep track of when you iterated through the entire block (messagesLeft), and the other will keep track of the current index of the message, which will be reduced by 1 if the message is deleted (as it will move one place on the line).

 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--; } } 

In response to your comment, here is a more complete example of how you can use the UID to delete without worrying about the numeric position / index.

 class Email { int UID { get; set; } DateTime Sent { get; set; } public string Body { get; set; } // put whichever properties you will need } List<Email> GetEmails(string mailbox); { Mailbox box = client.AllMailboxes[mailbox]; Fetch fetch = box.Fetch; List<Email> list = new List<Email>(); 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<Email> emails = GetEmails("inbox"); emails = emails.Where(email => email.Sent < DateTime.Now.AddHours(-hours)) foreach (Email email in emails) DeleteEmail(email); } 
+5
source

This is an official example from the documentation. instead of inbox.search ("all") change it to your search query, etc.

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; } } } } 
0
source

just add 1 to the post index.

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

Try deleting the last message first, then the second, and then proceed to delete the first message.

IF you delete and delete, changes to the message number have been set.

0
source

All Articles