简体   繁体   English

使用OpenPop.dll错误读取Gmail C#

[英]Reading gmail using OpenPop.dll errors c#

I'm trying to read an email body from a gmail in c# using OpenPop.dll. 我正在尝试使用OpenPop.dll从C#中的gmail读取电子邮件正文。 I've tried the gmail api but i could never get it working so I tried this one. 我已经尝试过gmail API,但是我无法使其正常运行,所以我尝试了这一步。 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. 在allMessages.Add部分给出错误。 (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]

You're probably using this code inside another for cycle and mistook the index variables. 您可能在另一个循环中使用此代码for并误认为了索引变量。 the parameter in the GetMessage method should be d , not i . GetMessage方法中的参数应该是d ,而不是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));
     }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM