EWS在收件箱中订阅流式传输通知

时间:2016-09-09 09:11:08

标签: c# exchangewebservices

我尝试编写一个控制台应用程序,它将使用EWS建立与邮箱的连接,然后在每次收到新电子邮件时打印一行。

一旦我完成这项工作,最终结果是将其转变为服务,并在每次电子邮件到达特定邮箱时创建任务,但是现在我无法让控制台在收到时写入一行。< / p>

控制台应用项目

Program.cs的

class Program
{
    static void Main(string[] args)
    {
         ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
         WebCredentials wbcred = new WebCredentials("myusername", "mypassw0rd");
         service.Credentials = wbcred;
         service.AutodiscoverUrl("myemailaddress@mydomain.com", RedirectionUrlValidationCallback);

         EWSConnection.SetStreamingNotifications(service);
    }

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

班级图书馆计划

EWSConnection.cs

  public static void SetStreamingNotifications(ExchangeService service)
        {
            StreamingSubscription subscription;

            // Subscribe to streaming notifications in the Inbox. 
            subscription = service.SubscribeToStreamingNotifications(
                new FolderId[] { WellKnownFolderName.Inbox },
                EventType.NewMail);

            // Create a streaming connection to the service object, over which events are returned to the client.
            // Keep the streaming connection open for 30 minutes.
            StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 30);
            connection.AddSubscription(subscription);
            connection.OnNotificationEvent += OnEvent;
            connection.OnDisconnect += OnDisconnect;
            connection.Open();

            bool status = connection.IsOpen;
            Console.WriteLine($"Connection Open:{status}");
        }

如果需要,我可以添加OnEventOnDisconnect方法。发生了什么是控制台打印

  

Connection Open:True按任意键继续。 。

然后,当我向该邮箱发送电子邮件时,没有任何事情发生,没有触发任何断点,也没有任何内容输出到控制台,这就是这两种方法的作用。

为什么我的OnEvent方法没有解雇?

编辑 - OnEvent&amp; OnDisconnect方法

 public 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————-Mail created:————-");
                        break;
                    case EventType.Created:
                        Console.WriteLine("\n————-Item or folder created:————-");
                        break;
                    case EventType.Deleted:
                        Console.WriteLine("\n————-Item or folder deleted:————-");
                        break;
                }
                // Display the notification identifier. 
                if (notification is ItemEvent)
                {
                    // The NotificationEvent for an e-mail message is an ItemEvent. 
                    ItemEvent itemEvent = (ItemEvent)notification;
                    Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);
                }
                else
                {
                    // The NotificationEvent for a folder is an FolderEvent. 
                    FolderEvent folderEvent = (FolderEvent)notification;
                    Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);
                }
            }
        }

public static void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
    // Cast the sender as a StreamingSubscriptionConnection object.           
    StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
    // Ask the user if they want to reconnect or close the subscription. 
    ConsoleKeyInfo cki;
    Console.WriteLine("The connection to the subscription is disconnected.");
    Console.WriteLine("Do you want to reconnect to the subscription? Y/N");
    while (true)
    {
        cki = Console.ReadKey(true);
        {
            if (cki.Key == ConsoleKey.Y)
            {
                connection.Open();
                Console.WriteLine("Connection open.");
                break;
            }
            else if (cki.Key == ConsoleKey.N)
            {
                // The ReadKey in the Main() consumes the E. 
                Console.WriteLine("\n\nPress E to exit");
                break;
            }
        }
    }
}

2 个答案:

答案 0 :(得分:3)

令人讨厌的是,我错过了Console.ReadKey()方法。一旦我添加了这个,它就按预期工作了......

    static void Main(string[] args)
    {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013);
        WebCredentials wbcred = new WebCredentials("myusername", "mypassw0rd","myDomain");
        service.Credentials = wbcred;
        service.AutodiscoverUrl("myemailaddress@mydomain.com", RedirectionUrlValidationCallback);

        EWSConnection.SetStreamingNotifications(service);

        Console.ReadKey(); //<-- this was missing
    }

答案 1 :(得分:0)

将方法绑定到当前订阅

connection.OnNotificationEvent +=
                new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
connection.OnDisconnect +=
        new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect); 

这是example