Gmail S22.IMAP,无法获取Microsoft Exchange Server的新邮件通知
我需要有关使用S22.IMAP库获取Microsoft Exchange电子邮件服务器的新邮件通知的帮助。我已经成功获得了gmail的新邮件通知,而Microsoft Exchange电子邮件S22.IMAP没有抛出新邮件通知 当我打印gmail和Microsoft Exchange的客户端功能时,我得到以下信息:Gmail S22.IMAP,无法获取Microsoft Exchange Server的新邮件通知,gmail,exchange-server,imap,Gmail,Exchange Server,Imap,我需要有关使用S22.IMAP库获取Microsoft Exchange电子邮件服务器的新邮件通知的帮助。我已经成功获得了gmail的新邮件通知,而Microsoft Exchange电子邮件S22.IMAP没有抛出新邮件通知 当我打印gmail和Microsoft Exchange的客户端功能时,我得到以下信息: using System; using System.Collections.Generic; using System.Linq; using System.Text; using
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 + "-------------");
}
}
}
如何获取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);
}
}
}
我认为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 + "-------------");
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Microsoft.Exchange.WebServices.Data;
Net系统;
名称空间MX
{
班级计划
{
静态交换服务MDP邮箱;
静态void Main(字符串[]参数)
{
MDP邮箱=新的ExchangeService(ExchangeVersion.Exchange2010_SP2);
NetworkCredential nc=新的网络凭据(“登录”、“密码”);
nc.Domain=“SOMEDOMAIN”;
MdpMailBox.Credentials=nc;
MdpMailBox.AutodiscoverUrl(“LOGIN@SOMEglobal.com");
StreamingSubscription sc=MdpMailBox.SubscribeToStreamingNotifications(新FolderId[]{WellKnownFolderName.Inbox},EventType.NewMail);
StreamingSubscriptionConnection scn=新的StreamingSubscriptionConnection(MDP邮箱,1);
补充认购(sc);
scn.OnNotificationEvent+=新的StreamingSubscriptionConnection.NotificationEventDelegate(OneEvent);
scn.OnSubscriptionError+=新的StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
scn.OnDisconnect+=新的StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
scn.Open();
Console.WriteLine(“等待电子邮件”);
系统线程线程睡眠(600000);
}
静态私有void OnDisconnect(对象发送方、SubscriptionErrorEventArgs args args)
{
//将发件人强制转换为StreamingSubscriptionConnection对象。
Console.WriteLine(“----邮箱已断开---”);
}
静态void OnEvent(对象发送方、NotificationEventArgs args args)
{
StreamingSubscription subscription=args.subscription;
//循环浏览所有与项目相关的事件。
foreach(args.Events中的NotificationEvent通知)
{
开关(notification.EventType)
{
case EventType.NewMail:
Console.WriteLine(“\n--------------收到的新邮件:-------”;
打破
}
//显示通知标识符。
如果(通知为ItemEvent)
{
//电子邮件的NotificationEvent是ItemEvent。
ItemEvent=(ItemEvent)通知;
FindItemsResults fi=MdpMailBox.FindItems(WellKnownFolderName.Inbox,新项目视图(1));
Console.WriteLine(fi.Items[0].Subject);
Console.WriteLine(“\nItemId:+itemEvent.ItemId.UniqueId”);
}
其他的
{
//文件夹的NotificationEvent是FolderEvent。
FolderEvent FolderEvent=(FolderEvent)通知;
Console.WriteLine(“\nFolderId:+folderEvent.FolderId.UniqueId”);
}
}
}
静态void OnError(对象发送方、SubscriptionErrorEventArgs args args)
{
//处理错误条件。
异常e=args.Exception;
Console.WriteLine(“\n----邮箱错误----”+e.Message+“----”);
}
}
}
有关更多详细信息,请参阅同样的问题。这似乎是S22.imap的问题,因为Outlook通知与其他解决方案配合得很好