gpt4 book ai didi

c# - EWS 订阅收件箱中的流式通知

转载 作者:行者123 更新时间:2023-11-30 21:46:14 25 4
gpt4 key购买 nike

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

一旦我完成这项工作,最终结果就是将其变成一项服务,并且每次电子邮件到达某个邮箱时都会创建一个任务,但现在我无法让控制台在收到时写一行。

控制台应用程序项目

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 Press any key to continue . . .

然后,当我向该邮箱发送电子邮件时,没有任何反应,没有触发断点,也没有向控制台输出任何内容,这就是这两种方法所做的。

为什么我的 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("myemailaddress@mydomain.com", RedirectionUrlValidationCallback);

EWSConnection.SetStreamingNotifications(service);

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

关于c# - EWS 订阅收件箱中的流式通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39407926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com