Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8778233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:28:50+00:00 2026-06-13T19:28:50+00:00

I’m using MailSystem.NET and trying to delete a message from the server. The problem

  • 0

I’m using MailSystem.NET and trying to delete a message from the server. The problem is 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’s 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 index of the message so I can delete it?


Edit:

The problem with the suggestions using the standard 1-based loop is that the counter index won’t be in sync with the message index, since in my case I’m searching to retrieve a specific subset of messages only (as I understand it).

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:28:51+00:00Added an answer on June 13, 2026 at 7:28 pm

    You can try deleting by the UID which should be more reliable and unique to each message. This has worked well for me in the past.

    Edit: Since deleting the message causes all the indexes to shift down by one, you can use two separate counters. One to keep track of when you have iterated through the entire box (messagesLeft) and the other will keep track of the current message index which will be decreased by 1 if a message is deleted (since it would move up one place in 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 fuller example of how you can use the UID for deletion without being concerned with 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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.