我需要创建一个连接到IMAP服务器并侦听新邮件的服务(或控制台)应用程序.我目前正在尝试使用MailKit,但我无法从文档中找出如何实现这一目标.
我最接近的是这篇文章.从我所看到的,这篇文章是将连接设置为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.");
}
Run Code Online (Sandbox Code Playgroud)
Jan*_*rát -8
由于您实际上是在尝试编写 IMAP 客户端,因此必须阅读一些 RFC。特别是,RFC3501 将告诉您 anALERT是什么以及您应该用它做什么。
抱歉,要么选择另一个为您提供更高级别界面的库,要么学习如何使用 IMAP。
| 归档时间: |
|
| 查看次数: |
5915 次 |
| 最近记录: |