Reputation: 2693
I am writing a synchronisation tool which is going to send all the received and sent email messages to our PHP application built in CakePHP. For this I am making use of Streaming Notifications.
Now, for every smtp address I want to listen to I am creating a new service which is going to listen to "created" events in the "Inbox" and "SentItems" folder. I am doing this via an account that has the right to impersonate all these accounts.
The folders and events I'm talking about (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
};
Also, we are using Outlook365 which can be accessed via https://portal.outlook365.com where I can then login. When I create a new user in the "Outlook365" I can login to that freshly created account.
But, this is where my problem arises. When I start the tool and open my SubscriptionStreamConnection to that smtp address, it immediately triggers an event of the type ItemEvent
. So, I am assuming that is a valid event, just like a newly created email message in the inbox and sentitems folder. But this first event does not contain all the data I am asking for and only happens once! The second time I listen to the account the event isn't triggered, so I assume this is some sort of "Create inbox" event, but I am not sure how I can distinguish this from a normal event.
What I am asking for (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
);
What I am receiving (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><[email protected]></t:InternetMessageId>
</t:Message>
</m:Items>
</m:GetItemResponseMessage>
</m:ResponseMessages>
</m:GetItemResponse>
</s:Body>
</s:Envelope>
Please note: I changed the ID's for the sake of readability, I kept the /
in place, just in case they mean something.
As you can see the XML response does not contain all the properties I am asking for, a normal event however (when I send an email to that new account) I do receive a complete response, like this:
<?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>[email protected]</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>[email protected]</t:EmailAddress>
<t:RoutingType>SMTP</t:RoutingType>
<t:MailboxType>OneOff</t:MailboxType>
</t:Mailbox>
</t:From>
<t:InternetMessageId><[email protected]></t:InternetMessageId>
</t:Message>
</m:Items>
</m:GetItemResponseMessage>
</m:ResponseMessages>
</m:GetItemResponse>
</s:Body>
</s:Envelope>
Of course I can add a simple check if for example the Subject exists in this event, but that doesn't seem right to me.
Just to clarify, some my project "properties" are as follows:
This is the code of the method that handles the event:
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();
}
}
Note the _handleExchangeEventToCrm
method, this methods actually tries to get the EmailMessage (as per PropertySet above).
Just to clarify
Why is a new user triggering an ItemEvent
when I am listening to it via a StreamingSubscribtion
for the VERY FIRST time? In other words, how can I distinguish a "normal" email sent/received event from any other event which apparently is an "ItemEvent" as well?
I suspect it has to do with "mailbox creation", but I am not sure. Any help is greatly appreciated!
If anything is unclear, please ask so that I can clarify my question
Upvotes: 0
Views: 1044
Reputation: 1658
I just repro'd this and found that the first item I received on a new mailbox was a folder associated item (FAI). So nothing appears in my inbox in OWA/Outlook, but I received a notification for a CreatedEvent in my Inbox. You can determine that it's a folder associated item by checking the IsAssociated element. If IsAssociated is true, it's not a normal message.
Upvotes: 1