none
EWS MA: Search for new email with a subscription RRS feed

  • Question

  • Hi

    I'm using streaming subscriptions to be notified when new emails arrive. The NotificationEventArgs only contain ItemIds of the new emails, so I'm trying to then extract them using service.FindItems. Here's the entire code I'm using in an attempt to read the email message

    private EmailMessage getMailMessage(string mailboxAddress, ItemId messageId)
            {
                FolderId fi = new FolderId(WellKnownFolderName.Inbox, new Mailbox(mailboxAddress));
                ItemView view = new ItemView(20);
                view.PropertySet = new PropertySet(EmailMessageSchema.From, EmailMessageSchema.IsRead, EmailMessageSchema.Sender, EmailMessageSchema.Subject,
                    EmailMessageSchema.DateTimeReceived, EmailMessageSchema.HasAttachments);
                view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Descending);
                SearchFilter.SearchFilterCollection filter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or);
                List<SearchFilter> searchFilterCollection = new List<SearchFilter>();
                searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.Id, messageId.UniqueId));
                filter.AddRange(searchFilterCollection);
                try
                {
                    FindItemsResults<Item> findResult = service.FindItems(WellKnownFolderName.Inbox, filter, view);
                    foreach (Item item in findResult)
                    {
                        if (item is EmailMessage)
                            return item as EmailMessage;
                    }
                }
                catch (Exception e)
                {
                    log("Unable to get message " + messageId + " from " + mailboxAddress + ": " + e.Message, 2);
                }
                return null;
            }

    If I do this, I get an exception telling me that the value is invalid for this property. If I put the entire messageId to the filter, so

    searchFilterCollection.Add(new SearchFilter.IsEqualTo(EmailMessageSchema.Id, messageId));

    then the exception is: Validation failed.\r\nParameter name: searchFilter

    So, what am I supposed to use as filter to extract the message I was just informed has arrived in my inbox?

    Regards

    Stephan

    Monday, November 4, 2013 6:03 PM

All replies

  • The notification is giving you the ItemId of the Item that caused the notification which you can use to bind to the Item directly eg

    EmailMessage email = EmailMessage.Bind(service, new ItemId(StringItemId));

    You only need to use FindItems when you don't know the ItemId of a particular item.  

    Cheers
    Glen

    Tuesday, November 5, 2013 2:44 AM