gpt4 book ai didi

c# - MailKit : Obtaining new mail notification from IMAP server

转载 作者:太空狗 更新时间:2023-10-29 23:53:41 24 4
gpt4 key购买 nike

我需要创建一个连接到 IMAP 服务器并监听新邮件的服务(或控制台)应用程序。我目前正在尝试使用 MailKit,但我无法从文档中找出如何完成此操作。

我最接近的是 this邮政。据我所知,这篇文章将连接设置为 IDLE 模式,并偶尔发送 NoOP 以尝试保持连接有效。我不完全清楚的是如何接收新邮件通知。

MailKit 文档似乎表明有可用的 Alert 事件。我试过连接到那个,但这似乎没有解决任何问题。

这是我尝试过的:

    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.");
}

最佳答案

我找到了一个样本 here .我一直在 IMAPClient 上寻找某种事件,但没有任何与消息通知具体相关的事件。

但是,如示例所示,事件在 IMAPFolder 类中......现在我想到它是有道理的。

希望这对其他人有帮助。

关于c# - MailKit : Obtaining new mail notification from IMAP server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26370747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com