尝试使用打开的pop库c#删除电子邮件

时间:2018-05-04 10:23:59

标签: c# openpop

我正在尝试从电子邮箱中读取电子邮件,并在某种情况下删除其中一些电子邮件。出于这个原因,我使用开放的流行库。当我尝试迭代并删除电子邮件时,我收到以下错误:'无法访问已处置的OpenPop.Pop3.Pop3Client。对象名称:' OpenPop.Pop3.Pop3Client'。'

Pop3Client popClient = new Pop3Client();

popClient.Connect("mail.test.com", 110, false);
popClient.Authenticate("test.com", "testpass");

int messageCount = popClient.GetMessageCount();

List<OpenPop.Mime.Message> allMessages = new List<OpenPop.Mime.Message>(messageCount);

for (int i = messageCount; i > 0; i--)
{
    allMessages.Add(popClient.GetMessage(i));
}

for(int i = allMessages.Count() - 1; i > 0; i--)
{
    StringBuilder builder = new StringBuilder();

    OpenPop.Mime.MessagePart plainText = allMessages[i].FindFirstPlainTextVersion();

    if (plainText != null)
    {
        builder.Append(plainText.GetBodyAsText());
        string emailText = builder.ToString();
        string email = GetStringValue("Email Address: ", emailText).Trim();
    }

    if (email != "test@gmail.com)
    {
        popClient.DeleteMessage(i);
        popClient.Disconnect();           
    }

}

0 个答案:

没有答案