Click here to Skip to main content
15,892,059 members
Articles / Programming Languages / C#

EWS Mail Notifier

Rate me:
Please Sign up or sign in to vote.
4.50/5 (4 votes)
19 Apr 2010CPOL2 min read 74K   3K   17  
Simple mail notifier for Exchange
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EWSConsoleNotify
{
    class PushNotification
    {
        private Notify _parent;
        
        public PushNotification(Notify Parent)
        {
            _parent = Parent;
        }

        public SendNotificationResultType ProcessNotification(SendNotificationResponseType SendNotification)
        {          
            SendNotificationResultType result = new SendNotificationResultType(); // Notification's result
            bool unsubscribe = false; 

            ResponseMessageType[] responseMessages = SendNotification.ResponseMessages.Items; // Response messages

            foreach (ResponseMessageType responseMessage in responseMessages)
            {
                if (responseMessage.ResponseCode != ResponseCodeType.NoError)
                {                    
                    result.SubscriptionStatus = SubscriptionStatusType.Unsubscribe;
                    return result;
                }

                NotificationType notification = ((SendNotificationResponseMessageType)responseMessage).Notification;

                string subscriptionId = notification.SubscriptionId;
              
                // If subscription Id is not the current subscription Id, unsubscribe
                if (subscriptionId != _parent.SubscriptionId)
                {
                    unsubscribe = true;
                }
                else
                {
                    for (int c = 0; c < notification.Items.Length; c++)
                    {
                        // Get only new mail events
                        if (notification.ItemsElementName[c].ToString() == "NewMailEvent")
                        {
                            BaseObjectChangedEventType bocet = (BaseObjectChangedEventType)notification.Items[c];

                            _parent.ShowMessage(((ItemIdType)bocet.Item).Id);
                        }
                    }
                }           
            }

            if (unsubscribe)
            {
                result.SubscriptionStatus = SubscriptionStatusType.Unsubscribe;
            }
            else
            {
                result.SubscriptionStatus = SubscriptionStatusType.OK;
            }


            return result;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Spain Spain
MCTS: .NET Framework 3.5 ASP.NET Applications

Comments and Discussions