添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I need to create a service (or console) app that connects to an IMAP server and listens for new mail. I'm currently trying to use MailKit but I'm having trouble trying to figure out how to accomplish this from the documentation.

The closest I've come is from this post. From what I can see, this post is setting the connection into IDLE mode and occasionally sending NoOP to try to keep the connection alive. What isn't entirely clear to me is how to receive the new mail notification.

The MailKit documentation seems to indicate that there is an Alert event available. I've tried hooking into that but that doesn't seem to fix anything.

Here is what I've tried:

    var cts = new CancellationTokenSource();
    testIDLEMailNotification(cts.Token);
    private static void testIDLEMailNotification(CancellationToken token)
        using (var client = ClientCreateAndConnect(token))
            while(!token.IsCancellationRequested)
                using (var done = new CancellationTokenSource())
                    using (var timer = new System.Timers.Timer(9 * 60 * 1000))
                        timer.Elapsed += (sender, e) => done.Cancel();
                        timer.AutoReset = false;
                        timer.Enabled = true;
                            client.Idle(done.Token, token);
                            client.NoOp(token);
                        catch (OperationCanceledException)
                            break;
    private static ImapClient ClientCreateAndConnect(CancellationToken token)
        var client = new ImapClient();
        client.Connect("server.domain", 993, true, token);
        client.AuthenticationMechanisms.Remove("XOAUTH");
        client.Authenticate("mailbox@server.domain", "password",token);
        client.Inbox.Open(MailKit.FolderAccess.ReadWrite, token);
        client.Alert += client_Alert;
        return client;
    static void client_Alert(object sender, AlertEventArgs e)
        Console.WriteLine("New Email or something.");

I found a sample here. I kept looking on the IMAPClient for some sort of event and there weren't any specifically relating to message notification.

However, as the sample shows, the events are on the IMAPFolder class ... which makes sense now that I think about it.

Hope this helps someone else.

As you're essentially trying to write an IMAP client, reading a couple of RFCs is mandatory. In particular, RFC3501 will tell you what an ALERT is and what you are supposed to do with it.

Sorry, either pick another library which presents you with a higher-level interface, or learn how to use IMAP.

Mailkit is quite good and will do what the OP wants. The link in the question even says it includes an example program called "imapidle". – arnt Oct 15, 2014 at 10:07 RFC3501 does not tell me anything about implementing an IMAP client with MailKit, which is what my question was about. My question was about MailKit and how MailKit implements handling new mail notifications. I agree that RFC3501 would possibly help my understanding of IMAP but its not going to do anything to help me with MailKit. Yes, I could use a different library, but when I have the lion's share of the code written and working, I don't think its unreasonable to ask on SO to see if someone else (maybe the author) knows how to implement it. – RHarris Oct 15, 2014 at 12:39 The problem with the code you showed is that it was trying to consume pretty raw IMAP events, not those events already translated into a high-level state of the remote mailbox. It seems that you found the part of MailKit which implements the interface you need, which is good. – Jan Kundrát Oct 17, 2014 at 19:55

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.