andyedinborough / aenetmail

C# POP/IMAP Mail Client
370 stars 153 forks source link

Cannot Perform .Search() when #172

Closed Dani21 closed 3 years ago

Dani21 commented 10 years ago

I've been testing out ImapClient, and it seems whenever I attach an event handler to it (more specifically .NewMessage), it prevents .Search() from ever working. When I try calling .Search(), it'll throw a NotSupportedException inside of TextClient.GetResponse() with the message "The Read method cannot be called when another read operation is pending." It may be something related to using Ssl since I have it turned on and SslStream is in the stack trace, but I'm not sure.

Anyways, here's an example.

using (var client = new AE.Net.Mail.ImapClient("imap.gmail.com", "email@gmail.com", "password", AE.Net.Mail.AuthMethods.Login, 993, true))
{
    client.NewMessage += (sender, e) => Console.WriteLine("New Mail!");
    try
    {
        var unseen = client.Search(AE.Net.Mail.SearchCondition.Unseen());
        Console.WriteLine("Searched.");
    }
    except (Exception)
    {
        Console.WriteLine("Failed.");
    }
}

Simply commenting out the .NewMessage line works. Commenting out the client.Search() line, adding Thread.Sleep(10000);, and receiving an email works (showing "New Mail!"). So both lines are functional, but .Search() isn't if .NewMessage is set.

Interestingly if I do something like this (where I temporarily remove the event handler), it works.

using (var client = new AE.Net.Mail.ImapClient("imap.gmail.com", "email@gmail.com", "password", AE.Net.Mail.AuthMethods.Login, 993, true))
{
    EventHandler<MessageEventArgs> evnt = null;
    evnt = (sender, e) =>
    {
        client.NewMessage -= evnt;
        try
        {
            var unseen = client.Search(AE.Net.Mail.SearchCondition.Unseen());
            Console.WriteLine("Searched.");
        }
        except (Exception)
        {
            Console.WriteLine("Failed.");
        }
        client.NewMessage += evnt;
    };
    client.NewMessage += evnt;
    Thread.Sleep(Timeout.Infinite);
}

So that's currently my workaround, but it's really counter-intuitive.

Dani21 commented 3 years ago

My comment was made almost 7 years ago... You are wasting your time replying to this issue.