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: using (var adapter = new ExchangeWebServicesAdapter(new Uri("https://w2k3x64/ews/exchange.asmx"),
21: new NetworkCredential("administrator", "password", "contoso")))
22: using (var listener = new PushNotificationListener())
23: {
24: Console.Out.WriteLine("Starting Notification Service...");
25: listener.Start();
26:
27: SubscriptionCollection subscriptionCollection;
28:
29: if (File.Exists(StateSaverFilename))
30: {
31: using (var stream = File.OpenRead(StateSaverFilename))
32: {
33: subscriptionCollection = SubscriptionCollection.Deserialize(stream, adapter, listener);
34: subscriptionCollection.SubscriptionRestartCompleted +=
35: SubscriptionCollection_OnSubscriptionRestartCompleted;
36: subscriptionCollection.SubscriptionRestartProgressChanged +=
37: SubscriptionCollection_OnSubscriptionRestartProgressChanged;
38:
39: subscriptionCollection.RestartAsync();
40: }
41: }
42: else
43: {
44: // Create a new subscription collection to manage all the subscriptions
45: // Register for a NewMail notification on the inbox of the administrator
46: subscriptionCollection = new SubscriptionCollection(adapter, listener)
47: {
48: {new[] {new FolderReference(WellKnownFolderId.Inbox)}, EventTypes.All}
49: };
50: }
51:
52: Console.Out.WriteLine("Creating subscription");
53: foreach (var subscription in subscriptionCollection)
54: {
55: // Write a line to the console for each new mail received 38:
56: subscription.NewMail += (sender, e) =>
57: Console.Out.WriteLine(string.Format("{0}: New Mail arrived in your inbox", e.Timestamp));
58: subscription.Start();
59: }
60: Console.Out.WriteLine("Waiting for notifications... Hit [Enter] to quit...");
61: Console.ReadLine();
62:
63: Console.Out.WriteLine("Saving the current state of the notification listener...");
64: using (var stream = File.OpenWrite(StateSaverFilename))
65: {
66: subscriptionCollection.Serialize(stream);
67: }
68: Console.Out.WriteLine("State saved to {0}", Path.GetFullPath(StateSaverFilename));
69: }
70: }
71:
72:
73: private static void SubscriptionCollection_OnSubscriptionRestartProgressChanged(object sender, ProgressChangedEventArgs args)
74: {
75: Console.Out.WriteLine("Subscription restart {0}% complete.", args.ProgressPercentage);
76: }
77:
78: private static void SubscriptionCollection_OnSubscriptionRestartCompleted(object sender, AsyncCompletedEventArgs<SubscriptionRestartErrorSummary> args)
79: {
80: Console.Out.WriteLine("Subscription restart is complete. {0} subscriptions could not be restarted.", args.Result.Errors.Count);
81: }
82: }
83: }