0

我正在尝试使用 OpenPop.dll 从 C# 中的 gmail 读取电子邮件正文。我已经尝试过 gmail api,但我永远无法让它工作,所以我尝试了这个。无论如何,这是我的代码和我得到的错误。

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

allMessages.Add 部分给出的错误。(运行)

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]
4

1 回答 1

1

您可能在另一个for循环中使用此代码并错误地使用了索引变量。方法中的参数GetMessage应该是d,不是i。它应该如下所示:

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));
     }
}
于 2016-01-18T22:46:58.353 回答