EWS subscribe to streaming notifications in the inbox
<p>I trying to write a Console Application which will establish a connection to a mailbox using EWS and then print a line every time a new email is received.</p>

<p>The end result once I have this working is to turn this into a service, and have a task created every time an email arrives in a certain mailbox, but for now I cant get the Console to write a line upon receipt.</p>

<p><strong>Console Application Project</strong></p>

<p><strong>Program.cs</strong></p>

<pre><code>class Program
{
    static void Main(string[] args)
    {
         ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
         WebCredentials wbcred = new WebCredentials("myusername", "mypassw0rd");
         service.Credentials = wbcred;
         service.AutodiscoverUrl("myemailaddress@mydomain.com", RedirectionUrlValidationCallback);

         EWSConnection.SetStreamingNotifications(service);
    }

    internal static bool RedirectionUrlValidationCallback(string redirectionUrl)
    {
        //The default for the validation callback is to reject the URL
        bool result = false;

        Uri redirectionUri = new Uri(redirectionUrl);
        if (redirectionUri.Scheme == "https")
        {
            result = true;
        }
        return result;
    } 
}
</code></pre>

<p><strong>Class Library Project</strong></p>

<p><strong>EWSConnection.cs</strong></p>

<pre><code>  public static void SetStreamingNotifications(ExchangeService service)
        {
            StreamingSubscription subscription;

            // Subscribe to streaming notifications in the Inbox. 
            subscription = service.SubscribeToStreamingNotifications(
                new FolderId[] { WellKnownFolderName.Inbox },
                EventType.NewMail);

            // Create a streaming connection to the service object, over which events are returned to the client.
            // Keep the streaming connection open for 30 minutes.
            StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 30);
            connection.AddSubscription(subscription);
            connection.OnNotificationEvent += OnEvent;
            connection.OnDisconnect += OnDisconnect;
            connection.Open();

            bool status = connection.IsOpen;
            Console.WriteLine($"Connection Open:{status}");
        }
</code></pre>

<p>I can add the <code>OnEvent</code> and <code>OnDisconnect</code> methods if required. What's happening is the console prints </p>

<blockquote>
  <p>Connection Open:True Press any key to continue . . .</p>
</blockquote>

<p>Then, when I send an email to that mailbox nothing happens, no break points are triggered and nothing is output to the console, which is what those two methods do.</p>

<p>Why is my <code>OnEvent</code> method not firing?</p>

<p><strong>Edit - OnEvent &amp; OnDisconnect methods</strong></p>

<pre><code> public 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————-Mail created:————-");
                        break;
                    case EventType.Created:
                        Console.WriteLine("\n————-Item or folder created:————-");
                        break;
                    case EventType.Deleted:
                        Console.WriteLine("\n————-Item or folder deleted:————-");
                        break;
                }
                // Display the notification identifier. 
                if (notification is ItemEvent)
                {
                    // The NotificationEvent for an e-mail message is an ItemEvent. 
                    ItemEvent itemEvent = (ItemEvent)notification;
                    Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);
                }
                else
                {
                    // The NotificationEvent for a folder is an FolderEvent. 
                    FolderEvent folderEvent = (FolderEvent)notification;
                    Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);
                }
            }
        }
</code></pre>

<p>and</p>

<pre><code>public static void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
    // Cast the sender as a StreamingSubscriptionConnection object.           
    StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
    // Ask the user if they want to reconnect or close the subscription. 
    ConsoleKeyInfo cki;
    Console.WriteLine("The connection to the subscription is disconnected.");
    Console.WriteLine("Do you want to reconnect to the subscription? Y/N");
    while (true)
    {
        cki = Console.ReadKey(true);
        {
            if (cki.Key == ConsoleKey.Y)
            {
                connection.Open();
                Console.WriteLine("Connection open.");
                break;
            }
            else if (cki.Key == ConsoleKey.N)
            {
                // The ReadKey in the Main() consumes the E. 
                Console.WriteLine("\n\nPress E to exit");
                break;
            }
        }
    }
}
</code></pre>