Search code examples
gmailexchange-serverimap

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


I need help in using S22.IMAP library to get a new mail notification for Microsoft Exchange email server. I've been successful in getting the new mail notification for gmail, and for Microsoft Exchange email S22.IMAP is not throwing the new mail notification.

When I print Client Capabilities for gmail and Microsoft Exchange I get the following:

enter image description here

How can I get new mail notification for Microsoft Exchange email?

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);
        }
    }
}

Solution

  • I think S22.IMAP library would not work for me, and I've settled down with the following:

    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("[email protected]");
    
                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 + "-------------");
            } 
        }
    }
    

    For more details refer to Exchange Server 2010 Documentation