我正在使用此处找到的 IMAP Idle 示例代码
该示例需要 Console.ReadKey 来取消 CancellationTokenSource,但建议只要该事件有权访问 CancellationTokenSource,就可以在新邮件到达时在 CountChanged 事件中取消它。
如何访问 CountChanged 事件中的 CancellationTokenSource?
这是上面链接中的代码片段......
// Keep track of changes to the number of messages in the folder (this is how we'll tell if new messages have arrived).
client.Inbox.CountChanged += (sender, e) => {
// Note: the CountChanged event will fire when new messages arrive in the folder and/or when messages are expunged.
var folder = (ImapFolder)sender;
Console.WriteLine("The number of messages in {0} has changed.", folder);
// Note: …Run Code Online (Sandbox Code Playgroud)