0

I'm trying to read an email body from a gmail in c# using OpenPop.dll. I've tried the gmail api but i could never get it working so I tried this one. Anyways, here is my code and the error I was given.

            using (Pop3Client client = new Pop3Client())
            {
                client.Connect("pop.gmail.com", 995, true);
                client.Authenticate("signalxxxxxx@gmail.com", "xxxxxxxx", AuthenticationMethod.UsernameAndPassword);
                int messageCount = client.GetMessageCount();
                List<OpenPop.Mime.Message> allMessages = new List<OpenPop.Mime.Message>(messageCount);
                for (int d = messageCount; d > 0; d--)
                {
                    allMessages.Add(client.GetMessage(i));
                }

Error Given on the allMessages.Add part. (runtime)

An unhandled exception of type 'OpenPop.Pop3.Exceptions.InvalidUseException' occurred in OpenPop.dll

Additional information: The messageNumber argument cannot have a value of zero or less. Valid messageNumber is in the range [1, messageCount]
dseds
  • 57
  • 1
  • 8
  • **The messageNumber argument cannot have a value of zero or less** it seems like you need to start at index 1 instead of 0 – ymz Jan 18 '16 at 22:42

1 Answers1

1

You're probably using this code inside another for cycle and mistook the index variables. the parameter in the GetMessagemethod should be d, not i. It should look like this:

using (Pop3Client client = new Pop3Client())
{
     client.Connect("pop.gmail.com", 995, true);
     client.Authenticate("signalxxxxxx@gmail.com", "xxxxxxxx", AuthenticationMethod.UsernameAndPassword);
     int messageCount = client.GetMessageCount();
     List<OpenPop.Mime.Message> allMessages = new List<OpenPop.Mime.Message>(messageCount);
     for (int d = messageCount; d > 0; d--)
     {
          allMessages.Add(client.GetMessage(d));
     }
}
Arturo Menchaca
  • 15,783
  • 1
  • 29
  • 53