我尝试编写一个控制台应用程序,它将使用 EWS 建立与邮箱的连接,然后在每次收到新电子邮件时打印一行。
一旦我完成这项工作,最终结果就是将它变成一项服务,并且每次电子邮件到达某个邮箱时都会创建一个任务,但现在我无法让控制台在收到时写一行。
控制台应用程序项目
程序.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("[email protected]", 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}");
}
如果需要,我可以添加
OnEvent
和 OnDisconnect
方法。发生的事情是控制台打印然后,当我向该邮箱发送电子邮件时,没有任何 react ,没有触发断点,也没有任何内容输出到控制台,这就是这两种方法的作用。
为什么我的
OnEvent
方法没有触发?编辑 - OnEvent 和 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;
}
}
}
}
最佳答案
令人讨厌的是,我错过了 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("[email protected]", RedirectionUrlValidationCallback);
EWSConnection.SetStreamingNotifications(service);
Console.ReadKey(); //<-- this was missing
}
关于c# - EWS 订阅收件箱中的流通知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39407926/