RHarris
RHarris

Reputation: 11167

MailKit : Obtaining new mail notification from IMAP server

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;
                        try
                        {
                            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("[email protected]", "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.");
    }

Upvotes: 9

Views: 11715

Answers (2)

RHarris
RHarris

Reputation: 11167

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.

Upvotes: 11

Jan Kundrát
Jan Kundrát

Reputation: 3816

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.

Upvotes: -9

Related Questions