简体   繁体   English

MailKit:从IMAP服务器获取新的邮件通知

[英]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. 我需要创建一个连接到IMAP服务器并侦听新邮件的服务(或控制台)应用程序。 I'm currently trying to use MailKit but I'm having trouble trying to figure out how to accomplish this from the documentation. 我目前正在尝试使用MailKit,但是在尝试从文档中找出如何完成此操作时遇到了麻烦。

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. 据我所知,这篇文章是将连接设置为IDLE模式,并偶尔发送NoOP来尝试保持连接状态。 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. MailKit文档似乎表明存在警报事件。 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("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. 我一直在IMAPClient上寻找某种事件,并且没有任何与消息通知特别相关的事件。

However, as the sample shows, the events are on the IMAPFolder class ... which makes sense now that I think about it. 但是,如示例所示,事件在IMAPFolder类上...现在考虑到这一点,这是有道理的。

Hope this helps someone else. 希望这对其他人有帮助。

As you're essentially trying to write an IMAP client, reading a couple of RFCs is mandatory. 实质上,您在尝试编写IMAP客户端时,必须读取几个RFC。 In particular, RFC3501 will tell you what an ALERT is and what you are supposed to do with it. 特别是,RFC3501会告诉您什么是ALERT ,以及您应该如何使用它。

Sorry, either pick another library which presents you with a higher-level interface, or learn how to use IMAP. 抱歉,选择另一个库为您提供更高级别的界面,或者学习如何使用IMAP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM