Outlook365 EWS流媒体通知|新用户优先事件
本文关键字:用户 事件 流媒体 EWS 通知 新用户 Outlook365 | 更新日期: 2023-09-27 18:03:10
我正在编写一个同步工具,它将发送所有收到和发送的电子邮件消息到我们的PHP应用程序内置在CakePHP。为此,我正在使用流式通知。
现在,对于我想要侦听的每个smtp地址,我正在创建一个新的服务,该服务将侦听"Inbox"answers"SentItems"文件夹中的"created"事件。我是通过一个有权冒充所有这些帐户的帐户来做这件事的。
我正在谈论的文件夹和事件 (c#)
// The folders we want to listen to
FolderId[] folderIds = new FolderId[] {
WellKnownFolderName.Inbox,
WellKnownFolderName.SentItems
};
// The event types we want to listen to
EventType[] eventTypes = new EventType[] {
EventType.Created
};
另外,我们正在使用Outlook365,可以通过https://portal.outlook365.com访问,然后我可以登录。当我在"Outlook365"中创建一个新用户时,我可以登录到新创建的帐户。
但是,这就是我的问题出现的地方。当我启动该工具并将SubscriptionStreamConnection打开到该smtp地址时,它会立即触发类型为ItemEvent
的事件。因此,我假设这是一个有效的事件,就像收件箱和sentitems文件夹中新创建的电子邮件消息一样。但是这第一个事件不包含我要求的所有数据,只发生一次!当我第二次收听帐户时,事件没有被触发,所以我假设这是某种"创建收件箱"事件,但我不确定如何将其与正常事件区分开来。
我所要求的 (c#)
PropertySet propSet = new PropertySet(
BasePropertySet.IdOnly,
EmailMessageSchema.InternetMessageId,
// Basic data
EmailMessageSchema.Subject,
EmailMessageSchema.UniqueBody,
EmailMessageSchema.ConversationId,
EmailMessageSchema.DateTimeReceived,
EmailMessageSchema.DateTimeSent,
// Sender and recipient data
EmailMessageSchema.From,
EmailMessageSchema.ToRecipients,
EmailMessageSchema.CcRecipients,
EmailMessageSchema.BccRecipients
);
我收到的信息 (XML)
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="939" MinorBuildNumber="16" Version="V2_11" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:GetItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:Items>
<t:Message>
<t:ItemId Id="AA/BB/CC/DD=" ChangeKey="AA/BB" />
<t:DateTimeReceived>2014-05-13T09:47:44Z</t:DateTimeReceived>
<t:DateTimeSent>2014-05-13T09:47:44Z</t:DateTimeSent>
<t:ConversationId Id="AA=" />
<t:InternetMessageId><11aa@AMXPR07MB104.eurprd07.prod.outlook.com></t:InternetMessageId>
</t:Message>
</m:Items>
</m:GetItemResponseMessage>
</m:ResponseMessages>
</m:GetItemResponse>
</s:Body>
</s:Envelope>
请注意:我为了可读性而改变了ID,我保留了/
,以防万一它们意味着。
正如您所看到的,XML响应不包含我要求的所有属性,但是在正常事件中(当我向新帐户发送电子邮件时),我确实收到了完整的响应,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="939" MinorBuildNumber="16" Version="V2_11" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:GetItemResponseMessage ResponseClass="Success">
<m:ResponseCode>NoError</m:ResponseCode>
<m:Items>
<t:Message>
<t:ItemId Id="AA/BB/CC/DE=" ChangeKey="AA/BB" />
<t:Subject>My random test!</t:Subject>
<t:DateTimeReceived>2014-05-13T10:25:55Z</t:DateTimeReceived>
<t:DateTimeSent>2014-05-13T10:25:38Z</t:DateTimeSent>
<t:ConversationId Id="AA/LEGY=" />
<t:UniqueBody BodyType="Text" IsTruncated="false">Hello World</t:UniqueBody>
<t:ToRecipients>
<t:Mailbox>
<t:Name>a10 Test</t:Name>
<t:EmailAddress>a10@mytestdomain.nl</t:EmailAddress>
<t:RoutingType>SMTP</t:RoutingType>
<t:MailboxType>Mailbox</t:MailboxType>
</t:Mailbox>
</t:ToRecipients>
<t:From>
<t:Mailbox>
<t:Name>My Name - Company</t:Name>
<t:EmailAddress>canttell@example.com</t:EmailAddress>
<t:RoutingType>SMTP</t:RoutingType>
<t:MailboxType>OneOff</t:MailboxType>
</t:Mailbox>
</t:From>
<t:InternetMessageId><AA00@SRV-01.mycompany.local></t:InternetMessageId>
</t:Message>
</m:Items>
</m:GetItemResponseMessage>
</m:ResponseMessages>
</m:GetItemResponse>
</s:Body>
</s:Envelope>
当然,我可以添加一个简单的检查,例如,如果Subject存在于这个事件中,但这对我来说似乎是不对的。
澄清一下,我的一些项目"属性"如下:
- 我正在连接outlook365.com 我正在使用c#与。net 4.5框架和最新的EWS API
- 我有一个用户可以模拟所有其他用户,这样程序就不需要知道所有的密码
- 我添加了所有的ImpersonatedUserId的我想听,每一个冒充的用户都有自己的服务(我认为这个信息是过时的,但它可能会帮助你)
- 在我的"OnNotificationEvent"中,我尝试通过它的ID获得项目(在一个新线程中,以便主程序可以继续)
这是处理事件的方法的代码:
protected void _OnExchangeNotificationEvent(object sender, NotificationEventArgs args)
{
StreamingSubscriptionConnection connection = (StreamingSubscriptionConnection)sender;
StreamingSubscription subscription = args.Subscription;
// Loop through all item-related events.
foreach (NotificationEvent notification in args.Events)
{
// if the notification is NOT an item event, skip the event
if (!(notification is ItemEvent))
{
continue;
}
// Start a new thread so the main program can continue
Thread exchangeToCrmThread = new Thread(() => this._handleExchangeEventToCrm((ItemEvent)notification, args));
exchangeToCrmThread.Start();
}
}
注意_handleExchangeEventToCrm
方法,这个方法实际上试图获得EmailMessage(按照上面的PropertySet)。
澄清一下
为什么当我第一次通过StreamingSubscribtion
收听时,新用户会触发ItemEvent
?换句话说,我如何区分"正常"电子邮件发送/接收事件与任何其他事件,显然是一个"ItemEvent"?
我怀疑它与"邮箱创建"有关,但我不确定。任何帮助都非常感谢!
如果有不清楚的地方,请告诉我,以便我澄清我的问题
标题>我只是重复了一下,发现我在新邮箱中收到的第一个项目是文件夹关联项目(FAI)。因此,在OWA/Outlook中,我的收件箱中没有显示任何内容,但我在收件箱中收到了CreatedEvent的通知。您可以通过检查IsAssociated元素来确定它是一个文件夹关联项。如果IsAssociated为true,则不是正常消息。