OfficeDev / ews-managed-api

Other
584 stars 319 forks source link

ChangeKey is getting changed while sending an email #249

Open sowmya062 opened 4 years ago

sowmya062 commented 4 years ago

Hi, we are sending an email using ews operations. We are performing following sequence of operations to send an email using ews.

  1. Performing CreateItem operation with message disposition set as "SaveOnly" and distinguishedfolderid as "Sent Items", so that email is getting saved as draft email in "Sent Items" folder.
  2. Using the ItemId and Changekey returned in the CreateItem response to perform GetItem operation, to fetch the recent ItemId and Changekey.
  3. Using the ItemId and Changekey returned in the GetItem response to perform SendItem operation, to send an email by saving it in Sent Items folder.

In this process SendItem operation is getting failed sometimes, because the Changekey is getting changed after performing GetItem operation. change key is getting changed within milliseconds after performing GetItem operation. Is this the known issue ? can anyone help us with the workaround to solve this.

pkropachev commented 4 years ago

Hello!

This is expected behaviour especially for Exchange Online. Unfortunately, this is the nature of the ChangeKey. There are two ways to solve it:

1) I guess in case of getting fail response on SendItem request you receive IrresolvableConflictError. So, you can handle this response and send GetItem request again to get up-to-dated ChangeKey and then try to send SendItem request one more time. Solution is not so beautiful, but in case of sending separate EWS requests you don't have another way, unfortunately.

2) Use power of EWS managed API library. Use EmailMessage class to create and fill message and then send it in one request. https://msdn.microsoft.com/en-us/library/office/dd633668(v=exchg.80).aspx