【问题标题】:Outlook365 EWS streaming notifications | new user first eventOutlook365 EWS 流式通知 |新用户第一事件
【发布时间】:2014-05-23 04:24:40
【问题描述】:

我正在编写一个同步工具,它将所有接收和发送的电子邮件消息发送到我们用 CakePHP 构建的 PHP 应用程序。为此,我正在使用Streaming Notifications

现在,对于我想收听的每个 smtp 地址,我正在创建一个新服务,该服务将收听“收件箱”和“SentItems”文件夹中的“已创建”事件。我通过一个有权模拟所有这些帐户的帐户来执行此操作。

我所说的文件夹和事件(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>

当然,我可以添加一个简单的检查,例如主题是否存在于此事件中,但这对我来说似乎不正确。

澄清一下,我的一些项目“属性”如下:

  • 我连接到outlook365.com
  • 我正在使用带有 .NET4.5 框架和最新 EWS API 的 C#
  • 我有一个用户可以模拟所有其他用户,因此程序不需要知道所有密码
  • 我添加了所有我想听的 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”的事件区分开来?

我怀疑它与“邮箱创建”有关,但我不确定。非常感谢任何帮助!

如果有任何不清楚的地方,请询问,以便我澄清我的问题

【问题讨论】:

    标签: c# exchange-server exchangewebservices office365 ews-managed-api


    【解决方案1】:

    我刚刚复制了这个,发现我在新邮箱上收到的第一个项目是文件夹关联项目 (FAI)。因此,在 OWA/Outlook 的收件箱中没有出现任何内容,但我在收件箱中收到了 CreatedEvent 的通知。您可以通过检查 IsAssociated 元素确定它是文件夹关联项。如果 IsAssociated 为真,则不是正常消息。

    【讨论】:

    • 感谢您为此付出的努力来检查它!如果它确实为我解决了问题,我会试一试并将其标记为答案。我现在不能说,因为我们处于 Office365 用户限制。所以我可能需要一段时间才能确定它。为你 +1!
    猜你喜欢
    • 2019-01-06
    • 2015-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多