S22.IMAP,无法获取Microsoft Exchange Server的新邮件通知

时间:2015-03-30 07:41:30

标签: gmail exchange-server imap

在使用S22.IMAP库获取Microsoft Exchange电子邮件服务器的新邮件通知时,我需要帮助。我已成功获取gmail的新邮件通知,并且对于Microsoft Exchange电子邮件S22.IMAP没有抛出新邮件通知。

当我为gmail和Microsoft Exchange打印客户端功能时,我得到以下内容:

enter image description here

如何获取Microsoft Exchange电子邮件的新邮件通知?

using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Net.Mail;
using S22.Imap;

namespace Test {
    class Program {
        static void Main(string[] args)
        {
            //For gmail I'm getting the new mail notification
            //using (ImapClient Client = new ImapClient("imap.gmail.com", 993,
            // "login", "password", AuthMethod.Login, true))

            //For Microsoft exchange email I'm not getting new mail notification, even though it supports "IDLE" 
            using (ImapClient Client = new ImapClient("mail.exchangeglobal.com", 993,
             "login", "password", AuthMethod.Login, true))
            {
                // Should ensure IDLE is actually supported by the server
                if(Client.Supports("IDLE") == false) {
                    Console.WriteLine("Server does not support IMAP IDLE");
                    return;
                }

                Client.NewMessage += OnNewMessage;

                // Put calling thread to sleep. This is just so the example program does
                // not immediately exit.
                System.Threading.Thread.Sleep(6000000);
            }
        }

        static void OnNewMessage(object sender, IdleMessageEventArgs e)
        {
            Console.WriteLine("A new message arrived. Message has UID: "+
                e.MessageUID);

             //Fetch the new message's headers and print the subject line

            MailMessage m = e.Client.GetMessage( e.MessageUID, FetchOptions.HeadersOnly );

            Console.WriteLine("New message's subject: " + m.Subject);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我认为S22.IMAP图书馆对我不起作用,而且我已经解决了以下问题:

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

namespace MX
{
    class Program
    {
        static ExchangeService MdpMailBox;

        static void Main(string[] args)
        {
            MdpMailBox = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

            NetworkCredential nc = new NetworkCredential("LOGIN", "PASSWORD");
            nc.Domain = "SOMEDOMAIN";
            MdpMailBox.Credentials = nc;
            MdpMailBox.AutodiscoverUrl("LOGIN@SOMEglobal.com");

            StreamingSubscription sc = MdpMailBox.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox }, EventType.NewMail);
            StreamingSubscriptionConnection scn = new StreamingSubscriptionConnection(MdpMailBox, 1);
            scn.AddSubscription(sc);
            scn.OnNotificationEvent += new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
            scn.OnSubscriptionError += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
            scn.OnDisconnect += new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
            scn.Open();
            Console.WriteLine("Waiting for email");
            System.Threading.Thread.Sleep(600000);
        }

        static private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
        {
            // Cast the sender as a StreamingSubscriptionConnection object.           
            Console.WriteLine("---Mail Box Disconnected---");
        }

        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-------------New Mail received:-------------");
                        break;
                }
                // Display the notification identifier. 
                if (notification is ItemEvent)
                {
                    // The NotificationEvent for an email message is an ItemEvent. 
                    ItemEvent itemEvent = (ItemEvent)notification;
                    FindItemsResults<Item> fi = MdpMailBox.FindItems(WellKnownFolderName.Inbox, new ItemView(1));
                    Console.WriteLine(fi.Items[0].Subject);

                    Console.WriteLine("\nItemId: " + itemEvent.ItemId.UniqueId);
                }
                else
                {
                    // The NotificationEvent for a folder is a FolderEvent. 
                    FolderEvent folderEvent = (FolderEvent)notification;
                    Console.WriteLine("\nFolderId: " + folderEvent.FolderId.UniqueId);
                }
            }
        }
        static void OnError(object sender, SubscriptionErrorEventArgs args)
        {
            // Handle error conditions. 
            Exception e = args.Exception;
            Console.WriteLine("\n----MAIL BOX Error ---" + e.Message + "-------------");
        } 
    }
}

有关详细信息,请参阅Exchange Server 2010 Documentation