0
votes

I am using EWS to manage emails of a shared mailbox through a console application.

Once they have been processed they are moved to another folder under the shared inbox (after saving the message id to a SQL record).

Through a separate process I want to retrieve the email using the ID.

When use the code:

var email = EmailMessage.Bind(serviceInstance, new ItemId(id));

The service throws this exception:

Microsoft.Exchange.WebServices.Data.ServiceResponseException: 'The specified object was not found in the store., The process failed to get the correct properties.'

I know that the service user has permissions on the inbox and folder as the same service has previously read the inbox and moved the email.

1

1 Answers

1
votes

The reason according to the MS documentation is:

when you move or copy an email message into a different folder, a new item is created in the new folder with a unique item ID, and the original message is deleted.

Therefore you get the exception: The specified object was not found in the store.

How to work around it?

Before processing an email messasge, create a custom extended property and set it on the email message and save the email message, to share the new state with the EWS server.

Guid myPropertySetId = new Guid("{20B5C09F-7CAD-44c6-BDBF-8FCBEEA08544}"); 
ExtendedPropertyDefinition myExtendedPropertyDefinition = new ExtendedPropertyDefinition(myPropertySetId, "MyExtendedPropertyName", MapiPropertyType.String); 
message.SetExtendedProperty(myExtendedPropertyDefinition, "MyExtendedPropertyValueIdentifingAnEmailMessageToBeMoved");
message.SendAndSaveCopy(); 

Store the value of the myExtendedPropertyDefinition (in the above example: "MyExtendedPropertyValueIdentifingAnEmailMessageToBeMoved") before you move the email message. Then you can move the email message to its destination folder.

After the original email has been processed (moved > deleted), you can find the email message at its new destination by seaching for the custom property and the value you stored to identify the email message:

ItemView view = new ItemView(5); 
SearchFilter searchFilter = new SearchFilter.IsEqualTo(myExtendedPropertyDefinition, "MyExtendedPropertyValueIdentifingAnEmailMessageToBeMoved"); 
view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, myExtendedPropertyDefinition); 
FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.SentItems, searchFilter, view); 

Use for every email message a unique identifying value in order to get one search result.

Please note that this work around is the officially (by MS) suggested approach as described in the documentation.