MailKit : Obtaining new mail notification from IMAP server
Asked Answered
P

2

9

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.");
    }
Petronille answered 14/10, 2014 at 21:33 Comment(0)
P
11

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.

Petronille answered 15/10, 2014 at 13:9 Comment(1)
That's amazing! Thanks! The link is so helpfull.Iolaiolande
I
-9

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.

Izaak answered 15/10, 2014 at 0:21 Comment(3)
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".Disrespectable
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.Petronille
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.Cantankerous

© 2022 - 2024 — McMap. All rights reserved.