Search code examples
c#exchangewebservicesemail-client

EWS managed API Pull notification to watch the new email is not working


I am creating a application which will work as a window service in the back ground. This application will parse the new emails in the inbox and save the attachments. I tried streaming notification but as the connection disconnects after 30 mints I thought to use pull notifications. Below is the code which I debug but I don't see any output on console. As soon as I run the application it closes the console window so don't know if it is working. I want watch the new email as soon as it enters in the inbox so need some guidance how to achieve that.

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

namespace PullNotification
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
             WebCredentials wbcred = new WebCredentials(ConfigurationSettings.AppSettings["user"], ConfigurationSettings.AppSettings["PWD"]); 
            PullSubscription SubscriptionInbox;

            service.Credentials = wbcred;
            service.AutodiscoverUrl(ConfigurationSettings.AppSettings["user-id"], RedirectionUrlValidationCallback);


            SubscriptionInbox = service.SubscribeToPullNotifications(new FolderId[] { WellKnownFolderName.Inbox }, 5/* subcription will end if the server is not polled within 5 mints*/, null/*to start a new subcription*/, EventType.NewMail, EventType.Modified);
            //Timer myTimer = new Timer();
            //myTimer.Elapsed += new ElapsedEventHandler(GetPullNotifications);
            //myTimer.Interval = 10000;
            //myTimer.Start();


            GetEventsResults events = SubscriptionInbox.GetEvents();
            EmailMessage message;

            foreach (ItemEvent itemEvent in events.ItemEvents)
            {
                switch (itemEvent.EventType)
                {
                    case EventType.NewMail:
                        try
                        {
                            Item item = Item.Bind(service, itemEvent.ItemId);
                            if (item.Subject == "A123")
                            {
                                Console.WriteLine("check the code");
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("error=" + e.Message);
                        }
                        break;
                    case EventType.Deleted:
                        Item item1 = Item.Bind(service, itemEvent.ItemId);
                        Console.WriteLine("Mail with subject" + item1.Subject + "--is deleted");
                        break;
                }
            }
            //Loop 




               }





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



    }
}

Solution

  • Pull notifications require that you issue a pull request (via GetEvents) when you want updates the difference between the notification methods is described in https://msdn.microsoft.com/en-us/library/office/dn458791%28v=exchg.150%29.aspx .Your code has no loop and only issue one GetItem request which is why it behaves the way you describe which is normal.

    I want watch the new email as soon as it enters in the inbox so need some guidance how to achieve that.

    If you want the Server to notify you when an email has arrived then you need to either look at Push or Streaming notifications Pull notifications requires that you poll the server for updates.

    I tried streaming notification but as the connection disconnects after 30 mints

    That's normal you just need code to reconnect and manage the subscription see http://blogs.msdn.com/b/emeamsgdev/archive/2013/04/16/ews-streaming-notification-sample.aspx for a good sample.

    Cheers Glen