S22.IMAP,无法获取 Microsoft Exchange Server 的新邮件通知

S22.IMAP, unable to get new mail notification for Microsoft Exchange Server

我在使用 S22.IMAP 库获取 Microsoft Exchange 电子邮件服务器的新邮件通知方面需要帮助。我已成功获得 gmail 的新邮件通知,而 Microsoft Exchange 电子邮件 S22.IMAP 未抛出新邮件通知。

当我打印 gmail 和 Microsoft Exchange 的客户端功能时,我得到以下信息:

如何获取 Microsoft Exchange 电子邮件的新邮件通知?

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Net.Mail;
using S22.Imap;

namespace Test {
    class Program {
        static void Main(string[] args)
        {
            //For gmail I'm getting the new mail notification
            //using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
            // "login", "password", AuthMethod.Login, true))

            //For Microsoft exchange email I'm not getting new mail notification, even though it supports "IDLE" 
            using (ImapClient Client = new ImapClient("mail.exchangeglobal.com", 993,
             "login", "password", AuthMethod.Login, true))
            {
                // Should ensure IDLE is actually supported by the server
                if(Client.Supports("IDLE") == false) {
                    Console.WriteLine("Server does not support IMAP IDLE");
                    return;
                }

                Client.NewMessage += OnNewMessage;

                // Put calling thread to sleep. This is just so the example program does
                // not immediately exit.
                System.Threading.Thread.Sleep(6000000);
            }
        }

        static void OnNewMessage(object sender, IdleMessageEventArgs e)
        {
            Console.WriteLine("A new message arrived. Message has UID: "+
                e.MessageUID);

             //Fetch the new message's headers and print the subject line

            MailMessage m = e.Client.GetMessage( e.MessageUID, FetchOptions.HeadersOnly );

            Console.WriteLine("New message's subject: " + m.Subject);
        }
    }
}

我认为 S22.IMAP 库不适合我,我已经解决了以下问题:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Exchange.WebServices.Data;
using System.Net;

namespace MX
{
    class Program
    {
        static ExchangeService MdpMailBox;

        static void Main(string[] args)
        {
            MdpMailBox = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

            NetworkCredential nc = new NetworkCredential("LOGIN", "PASSWORD");
            nc.Domain = "SOMEDOMAIN";
            MdpMailBox.Credentials = nc;
            MdpMailBox.AutodiscoverUrl("LOGIN@SOMEglobal.com");

            StreamingSubscription sc = MdpMailBox.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);
            StreamingSubscriptionConnection scn = new StreamingSubscriptionConnection(MdpMailBox, 1);
            scn.AddSubscription(sc);
            scn.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
            scn.OnSubscriptionError += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
            scn.OnDisconnect += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
            scn.Open();
            Console.WriteLine("Waiting for email");
            System.Threading.Thread.Sleep(600000);
        }

        static private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
        {
            // Cast the sender as a StreamingSubscriptionConnection object.           
            Console.WriteLine("---Mail Box Disconnected---");
        }

        static void OnEvent(object sender, NotificationEventArgs args)
        {
            StreamingSubscription subscription = args.Subscription;

            // Loop through all item-related events. 
            foreach (NotificationEvent notification in args.Events)
            {

                switch (notification.EventType)
                {
                    case EventType.NewMail:
                        Console.WriteLine("\n-------------New Mail received:-------------");
                        break;
                }
                // Display the notification identifier. 
                if (notification is ItemEvent)
                {
                    // The NotificationEvent for an email message is an ItemEvent. 
                    ItemEvent itemEvent = (ItemEvent)notification;
                    FindItemsResults<Item> fi = MdpMailBox.FindItems(WellKnownFolderName.Inbox, new ItemView(1));
                    Console.WriteLine(fi.Items[0].Subject);

                    Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);
                }
                else
                {
                    // The NotificationEvent for a folder is a FolderEvent. 
                    FolderEvent folderEvent = (FolderEvent)notification;
                    Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);
                }
            }
        }
        static void OnError(object sender, SubscriptionErrorEventArgs args)
        {
            // Handle error conditions. 
            Exception e = args.Exception;
            Console.WriteLine("\n----MAIL BOX Error ---" + e.Message + "-------------");
        } 
    }
}

详情请参考Exchange Server 2010 Documentation