【发布时间】:2015-01-23 18:23:12
【问题描述】:
我正在尝试创建一个将在后台连续运行的进程。此服务将查看收件箱中传入的新电子邮件并解析电子邮件。我们有 Exchange Server,所以我使用的是 Exchange Managed API。如 EWS 文档中所述,我创建了一个 C# 类。我有单独测试过的服务实例。我正在收件箱上创建流式通知和新邮件事件。但我不知道如何测试应用程序,因为我关闭了应用程序控制台窗口,我手动向我的收件箱发送了一封邮件,但不知道新邮件事件何时会触发以及它是否会在控制台上显示消息。我不确定我们是否需要在服务器端存在或配置某些东西。我想知道是否有任何方法可以让这个过程在没有 30 分钟的情况下运行。我想创建服务的时间间隔。请寻求帮助。
namespace NewMailNotification
{
class Program
{
static void Main(string[] args)
{
ExchangeService service = new ExchangeService (ExchangeVersion.Exchange2010_SP2);
service.Url = null;
string user = ConfigurationSettings.AppSettings["user"];
string userid = ConfigurationSettings.AppSettings["user-id"];
string PWD = ConfigurationSettings.AppSettings["PWD"];
try
{
service.Credentials = new WebCredentials(user.ToString(), PWD.ToString());
service.AutodiscoverUrl(userid.ToString(), RedirectionUrlValidationCallback);
}
catch (Exception e)
{
Console.WriteLine("---" + e.Message);
}
SetStreamingNotification(service);
}
private static bool RedirectionUrlValidationCallback(string RedirectionUrl)
{
bool result = false;
Uri redirectionUri = new Uri(RedirectionUrl);
if (redirectionUri.Scheme == "https")
{
Console.WriteLine(redirectionUri);
result = true;
}
return result;
}
//Notification subscription and event
static void SetStreamingNotification(ExchangeService service)
{
//subscribe to streaming notification onthe inbox folder, and listen to newmail
StreamingSubscription streamingsubscription = service.SubscribeToStreamingNotifications(new FolderId[] { WellKnownFolderName.Inbox },
EventType.NewMail,
EventType.Created,
EventType.Deleted);
StreamingSubscriptionConnection connection = new StreamingSubscriptionConnection(service, 30);
connection.AddSubscription(streamingsubscription);
//Delegate an event handlers
connection.OnNotificationEvent +=
new StreamingSubscriptionConnection.NotificationEventDelegate(OnEvent);
connection.OnSubscriptionError +=
new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnError);
connection.OnDisconnect +=
new StreamingSubscriptionConnection.SubscriptionErrorDelegate(OnDisconnect);
connection.Open();
Console.WriteLine("--------- StreamSubscription event -------");
}
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 Received-----");
break;
case EventType.Created:
Console.WriteLine("\n-------------Item or Folder deleted-------");
break;
case EventType.Deleted:
Console.WriteLine("\n------------Item or folder deleted---------");
break;
}
//Display the notification identifier.
if (notification is ItemEvent)
{
//The notificationEvent for a folder is a Folderevent.
FolderEvent folderevent = (FolderEvent)notification;
Console.WriteLine("\nFolderId: " + folderevent.FolderId.UniqueId);
}
else
{
//The notificationevent for a foler is a FolderEvent
FolderEvent folderevent = (FolderEvent)notification;
Console.WriteLine("\nFolderId: " + folderevent.FolderId.UniqueId);
}
}
}
static private void OnDisconnect(object sender, SubscriptionErrorEventArgs args)
{
StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
//ask the usr if they want to reconnect or close the connection
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.");
Console.WriteLine("\r\n");
break;
}
else if (cki.Key == ConsoleKey.N)
{
// Signal.Set();
bool isOpen = connection.IsOpen;
if (isOpen == true)
{
connection.Close();
}
else
{
break;
}
}
}//while end
}
}
static void OnError(object sender,SubscriptionErrorEventArgs args)
{
Exception e=args.Exception;
Console.WriteLine("\n------------Error----"+e.Message+"----------");
}
}//class end
}//命名空间结束
【问题讨论】:
标签: c# .net email exchangewebservices exchange-server-2010