-2
private int numberofallmessages = 0;
        private int countMsg = 0;

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            OpenPop.Pop3.Pop3Client PopClient = new OpenPop.Pop3.Pop3Client();
            PopClient.Connect("mail", 110, false);
            PopClient.Authenticate("r", "n",
                OpenPop.Pop3.AuthenticationMethod.UsernameAndPassword);
            List<string> uids = PopClient.GetMessageUids();
            int messageCount = PopClient.GetMessageCount();
            numberofallmessages = messageCount;
            allMessages = new List<OpenPop.Mime.Message>(messageCount);
            for (int i = messageCount; i > 0; i--)
            {
                if (backgroundWorker1.CancellationPending == true)
                {
                    e.Cancel = true;
                    return;
                }
                string currentUidOnServer = uids[i];
                if (!seenUids.Contains(currentUidOnServer))
                {
                    if (i > 0)
                        allMessages.Add(PopClient.GetMessage(i));
                    SaveFullMessage(PopClient.GetMessage(i), i);
                    w = new StreamWriter(emailsIDSFile, true);
                    w.WriteLine(i + " = " + currentUidOnServer);
                    w.Close();
                    int nProgress = (messageCount - i + 1) * 100 / messageCount;
                    backgroundWorker1.ReportProgress(nProgress, PopClient.GetMessageCount().ToString() + "/" + i);
                }
            }
            PopClient.Disconnect();
        }

The problem is on the line:

string currentUidOnServer = uids[i];

uids contain 7047 items and also 'i' is now on 7047

I see now that if i change the line:

int messageCount = PopClient.GetMessageCount();

To:

int messageCount = 30;

It's working fine. So why when its GetMessageCount() and it's 7047 and not 30 it's not working ?

Index was out of range. Must be non-negative and less than the size of the collection.

Shmuel Naukarkar
  • 233
  • 1
  • 2
  • 12
  • 2
    `List` uses 0-based indexing, so `list[list.Count]` is always invalid. – Jon Skeet Feb 29 '16 at 18:06
  • If it has 7047 members, index 7047 is not gonna be there. The indices go from 0 to 7046. – Carlos Feb 29 '16 at 18:06
  • Are you sure about that `int i = messageCount;`? I reckon there should be `int i = messageCount-1;` – Valentin Feb 29 '16 at 18:07
  • 1
    Possible duplicate of [What is IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it) – Valentin Feb 29 '16 at 18:08

2 Answers2

5

C# indices are zero-based (0 through N - 1). So if you had a 7047-element collection, the last item would be item[7046]. Attempting to go beyond the last element with item[7047] will cause the error you are getting.

Looking at your code, the following has two problems:

for (int i = messageCount; i > 0; i--)

First, the initial element will be one higher than the last item in the collection (which would cause the error you reported).

Second, the final (first) item in the collection will never be reached because this iteration will stop at element 1 when the last (or first) element is at 0.

Setting the index to 30 prevents the error because obviously that last index in the collection is not more than 30.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
1

This is because the available index of a collection goes from 0 to Collection.Length-1. So you should set messageCount to

int messageCount = PopClient.GetMessageCount()-1;
wajiro_91
  • 170
  • 1
  • 9