1: using System;
2: using System.ComponentModel;
3: using System.IO;
4: using System.Net;
5: using InfiniTec.Exchange.Notifications;
6: using InfiniTec.Threading;
7:
8: namespace NewMailNotificationExample
9: {
10: internal class Program
11: {
12: private const string _StateSaverFilename = "notificationstate.bin";
13:
14: private static void Main()
15: {
16: // Ignore any certificate errors
17: ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
18:
19: // Setup the adapter which will be used to call into the Exchange WebService
20: var adapter = new ExchangeWebServicesAdapter(new Uri("https://w2k3x64/ews/exchange.asmx"),
21: new NetworkCredential("administrator", "password", "contoso"));
22:
23:
24: // Setup a listener that listens on port 80 on the local computer
25:
26: using (var listener = new PushNotificationListener())
27: {
28: Console.Out.WriteLine("Starting Notification Service...");
29: listener.Start();
30:
31: SubscriptionCollection subscriptionCollection;
32:
33: if (File.Exists(_StateSaverFilename))
34: {
35: using (var stream = File.OpenRead(_StateSaverFilename))
36: {
37: subscriptionCollection = SubscriptionCollection.Deserialize(stream);
38: subscriptionCollection.SubscriptionRestartCompleted +=
39: SubscriptionCollection_OnSubscriptionRestartCompleted;
40: subscriptionCollection.SubscriptionRestartProgressChanged +=
41: SubscriptionCollection_OnSubscriptionRestartProgressChanged;
42:
43: subscriptionCollection.RestartAsync(listener, adapter);
44: }
45: }
46: else
47: {
48: // Create a new subscription collection to manage all the subscriptions
49: // Register for a NewMail notification on the inbox of the administrator
50: subscriptionCollection = new SubscriptionCollection(adapter)
51: {
52: {new[] {new FolderReference(WellKnownFolderId.Inbox)}, EventTypes.NewMail}
53: };
54: }
55:
56: Console.Out.WriteLine("Creating subscription");
57: foreach (var subscription in subscriptionCollection)
58: {
59: // Write a line to the console for each new mail received 38:
60: subscription.NewMail += (sender, e) =>
61: Console.Out.WriteLine(string.Format("{0}: New Mail arrived in your inbox", e.Timestamp));
62: subscription.Start(listener);
63: }
64: Console.Out.WriteLine("Waiting for notifications... Hit [Enter] to quit...");
65: Console.ReadLine();
66:
67: Console.Out.WriteLine("Saving the current state of the notification listener...");
68: using (var stream = File.OpenWrite(_StateSaverFilename))
69: {
70: subscriptionCollection.Serialize(stream);
71: }
72: Console.Out.WriteLine("State saved to {0}", Path.GetFullPath(_StateSaverFilename));
73: }
74: }
75:
76: private static void SubscriptionCollection_OnSubscriptionRestartProgressChanged(object sender, ProgressChangedEventArgs args)
77: {
78: Console.Out.WriteLine("Subscription restart {0}% complete.", args.ProgressPercentage);
79: }
80:
81: private static void SubscriptionCollection_OnSubscriptionRestartCompleted(object sender, AsyncCompletedEventArgs<SubscriptionRestartErrorSummary> args)
82: {
83: Console.Out.WriteLine("Subscription restart is complete. {0} subscriptions could not be restarted.", args.Result.Errors.Count);
84: }
85: }
86: }