我正在使用S22.IMAP实现显示here的示例,特别是NewMessage事件。
我正在从我的Gmail帐户向我的iCloud发送测试电子邮件,但事件未触发。我将其撤消并发送到我的Gmail,该事件被解雇了。 iCloud实际上不支持IMAP IDLE吗?它没有抛出异常,说它没有或是否有一些特殊的事情要让iCloud推送到您的应用程序?
public EmailInstance(String email, SupportedMailTypes type)
{
_emailAddress = email;
_mailType = type;
InitializeEmail();
}
private void InitializeEmail()
{
if (_mailType == SupportedMailTypes.Gmail)
{
_client = new ImapClient(GMAIL_SERVER, PORT, _emailAddress, PASSWORD, AuthMethod.Login, true);
}
else if (_mailType == SupportedMailTypes.iCloud)
{
_client = new ImapClient(ICLOUD_SERVER, PORT, _emailAddress, PASSWORD, AuthMethod.Login, true);
}
// Download mail messages from the default mailbox.
NewEmailCount = GetMessageCount();
// Check that IDLE is supported by the server
if (_client.Supports("IDLE") == false)
{
throw new Exception("Server does not support IDLE.");
}
_client.IdleError += OnIdleError;
_client.NewMessage += OnNewMessage;
_client.MessageDeleted += OnMessageDeleted;
}
/// <summary>
/// If idling throws an error.
/// </summary>
private void OnIdleError(object sender, IdleErrorEventArgs e)
{
throw new Exception(e.Exception.Message);
}
/// <summary>
/// What to do when the client receives a new message.
/// </summary>
private void OnNewMessage(object sender, IdleMessageEventArgs e)
{
NewEmailCount = GetMessageCount();
var header = _client.GetMessage(e.MessageUID, FetchOptions.HeadersOnly, false);
}
private void OnMessageDeleted(object sender, IdleMessageEventArgs e)
{
NewEmailCount = GetMessageCount();
}
/// <summary>
/// Returns the message count
/// </summary>
private int GetMessageCount()
{
var uids = _client.Search(SearchCondition.Unseen());
return _client.GetMessages(uids, FetchOptions.HeadersOnly, false).Count();
}