EWS reports SendItem as succeeding but message is still in Drafts folder
<p>I'm using the EWS Managed API to send emails via exchange. When sending an item with attachments I first do a <code>CreateItem</code> and then a <code>SendItem</code>. Mostly this works fine but sometimes items are left in the Drafts folder, even though EWS reports the <code>SendItem</code> succeeds. How can I tell what's going on here? </p>

<p>The EWS messages I see from tracing are first <code>CreateItem</code>:</p>

<pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soap:Header&gt;
      &lt;t:RequestServerVersion Version="Exchange2013" /&gt;
    &lt;/soap:Header&gt;
    &lt;soap:Body&gt;
      &lt;m:CreateItem MessageDisposition="SaveOnly"&gt;
        &lt;m:SavedItemFolderId&gt;
          &lt;t:DistinguishedFolderId Id="drafts"&gt;
            &lt;t:Mailbox&gt;
              &lt;t:EmailAddress&gt;amailbox@thedomain.com&lt;/t:EmailAddress&gt;
            &lt;/t:Mailbox&gt;
          &lt;/t:DistinguishedFolderId&gt;
        &lt;/m:SavedItemFolderId&gt;

        ...

      &lt;/m:CreateItem&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
&lt;/Trace&gt;
</code></pre>

<p>I get a Success response: </p>

<pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;s:Header&gt;
      &lt;h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="1104" MinorBuildNumber="3" Version="V2_22" 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" /&gt;
    &lt;/s:Header&gt;
    &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"&gt;
        &lt;m:ResponseMessages&gt;
          &lt;m:CreateItemResponseMessage ResponseClass="Success"&gt;
            &lt;m:ResponseCode&gt;NoError&lt;/m:ResponseCode&gt;
            &lt;m:Items&gt;
              &lt;t:Message&gt;
                &lt;t:ItemId Id="AAMkADExMmY0MTgzLWJmZmUtNDcxNi1iOTk0LWMzZTU3M2I3NjBkMwBGAAAAAABL/dBAdDByRbztYn12Cy/4BwCJZGY3HQ5zQpbF9WoRXD0sAAAAAAEPAACJZGY3HQ5zQpbF9WoRXD0sAAA6HNVuAAA=" ChangeKey="CQAAABYAAACJZGY3HQ5zQpbF9WoRXD0sAAA6JC4z" /&gt;
                &lt;t:Attachments&gt;
                  &lt;t:FileAttachment&gt;
                    &lt;t:AttachmentId Id="AAMkADExMmY0MTgzLWJmZmUtNDcxNi1iOTk0LWMzZTU3M2I3NjBkMwBGAAAAAABL/dBAdDByRbztYn12Cy/4BwCJZGY3HQ5zQpbF9WoRXD0sAAAAAAEPAACJZGY3HQ5zQpbF9WoRXD0sAAA6HNVuAAABEgAQAGiKX5lgtuFLgfIBgyg4IwM=" /&gt;
                  &lt;/t:FileAttachment&gt;
                  &lt;t:FileAttachment&gt;
                    &lt;t:AttachmentId Id="AAMkADExMmY0MTgzLWJmZmUtNDcxNi1iOTk0LWMzZTU3M2I3NjBkMwBGAAAAAABL/dBAdDByRbztYn12Cy/4BwCJZGY3HQ5zQpbF9WoRXD0sAAAAAAEPAACJZGY3HQ5zQpbF9WoRXD0sAAA6HNVuAAABEgAQADkNd4D6H0NAgxCM7uH6MGo=" /&gt;
                  &lt;/t:FileAttachment&gt;
                  &lt;t:FileAttachment&gt;
                    &lt;t:AttachmentId Id="AAMkADExMmY0MTgzLWJmZmUtNDcxNi1iOTk0LWMzZTU3M2I3NjBkMwBGAAAAAABL/dBAdDByRbztYn12Cy/4BwCJZGY3HQ5zQpbF9WoRXD0sAAAAAAEPAACJZGY3HQ5zQpbF9WoRXD0sAAA6HNVuAAABEgAQAKu2RdYFlBVIiLyPxshNCZQ=" /&gt;
                  &lt;/t:FileAttachment&gt;
                  &lt;t:FileAttachment&gt;
                    &lt;t:AttachmentId Id="AAMkADExMmY0MTgzLWJmZmUtNDcxNi1iOTk0LWMzZTU3M2I3NjBkMwBGAAAAAABL/dBAdDByRbztYn12Cy/4BwCJZGY3HQ5zQpbF9WoRXD0sAAAAAAEPAACJZGY3HQ5zQpbF9WoRXD0sAAA6HNVuAAABEgAQAJ2C2tyz2iJHm8XC9LmSkcA=" /&gt;
                  &lt;/t:FileAttachment&gt;
                &lt;/t:Attachments&gt;
              &lt;/t:Message&gt;
            &lt;/m:Items&gt;
          &lt;/m:CreateItemResponseMessage&gt;
        &lt;/m:ResponseMessages&gt;
      &lt;/m:CreateItemResponse&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
&lt;/Trace&gt;
</code></pre>

<p>I then ask to send it and move to SentItems: </p>

<pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;soap:Header&gt;
      &lt;t:RequestServerVersion Version="Exchange2013" /&gt;
    &lt;/soap:Header&gt;
    &lt;soap:Body&gt;
      &lt;m:SendItem SaveItemToFolder="true"&gt;
        &lt;m:ItemIds&gt;
          &lt;t:ItemId Id="AAMkADExMmY0MTgzLWJmZmUtNDcxNi1iOTk0LWMzZTU3M2I3NjBkMwBGAAAAAABL/dBAdDByRbztYn12Cy/4BwCJZGY3HQ5zQpbF9WoRXD0sAAAAAAEPAACJZGY3HQ5zQpbF9WoRXD0sAAA6HNVuAAA=" ChangeKey="CQAAABYAAACJZGY3HQ5zQpbF9WoRXD0sAAA6JC4z" /&gt;
        &lt;/m:ItemIds&gt;
        &lt;m:SavedItemFolderId&gt;
          &lt;t:DistinguishedFolderId Id="sentitems"&gt;
            &lt;t:Mailbox&gt;
              &lt;t:EmailAddress&gt;amailbox@thedomain.com&lt;/t:EmailAddress&gt;
            &lt;/t:Mailbox&gt;
          &lt;/t:DistinguishedFolderId&gt;
        &lt;/m:SavedItemFolderId&gt;
      &lt;/m:SendItem&gt;
    &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
&lt;/Trace&gt;
</code></pre>

<p>And I get a Success NoError response: </p>

<pre><code>  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;s:Header&gt;
      &lt;h:ServerVersionInfo MajorVersion="15" MinorVersion="0" MajorBuildNumber="1104" MinorBuildNumber="3" Version="V2_22" 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" /&gt;
    &lt;/s:Header&gt;
    &lt;s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
      &lt;m:SendItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"&gt;
        &lt;m:ResponseMessages&gt;
          &lt;m:SendItemResponseMessage ResponseClass="Success"&gt;
            &lt;m:ResponseCode&gt;NoError&lt;/m:ResponseCode&gt;
          &lt;/m:SendItemResponseMessage&gt;
        &lt;/m:ResponseMessages&gt;
      &lt;/m:SendItemResponse&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
&lt;/Trace&gt;
</code></pre>

<p>These are all traced by the same managed ThreadId so I'm pretty sure they all correspond to the same item. </p>

<p>I'm not using exchange impersonation. I'm authenticating with username &amp; password auth with credentials for the mailbox that is sending the items.</p>

<p><strong>EDIT (22 May):</strong> </p>

<p>I've added X-AnchorMailbox header to my requests and upgraded to EWS Managed API 2.2 (was using 2.1, although since the SOAP messages looked right I don't think this was an issue). The problem is still happening. </p>

<p>I added code to re-retrieve the item after sending, to check if it's still in Drafts folder. I do this through <code>EmailMessage.Bind()</code> with the Id of the item just sent from drafts. When I run against Exchange on Office 365 that always raises an exception because the item no longer exists, which is expected since the ItemId changes when the item moves folder. When I run against customer's Exchange server it finds the item by that Id. I then try to resend it and get a <code>ServiceResponseException</code> with message <code>Access is denied. Check credentials and try again.</code> If I try to resend a message against Office 365 I get a <code>SendItemResponse</code> with message <code>The specified object was not found in the store., The process failed to get the correct properties.</code> (SOAP message is <code>SendItemResponse</code> with ResponseCode <code>ErrorItemNotFound</code>.</p>

<p>I've raised a support request with Microsoft (which oddly hasn't been responded to in over 48 hours ... maybe there's some trick to raising support requests).</p>

<p><strong>EDIT (24 May)</strong></p>

<p>When repeating the tests I sometimes saw items that remain in Drafts BUT are also received by the recipient! When inspecting their Properties > Internet Headers via outlook they both have the same Message-ID. More indication that something is messed up on the Exchange side of things?</p>

<p><strong>EDIT (30 May)</strong></p>

<p>I heard from Microsoft support who explained a bit about how sending in exchange works and what they think is happening: </p>

<blockquote>
  <p>a No error response from EWS means that the message was
  successfully flagged for submissions (PR_MESSAGE_FLAGS >
  MSGFLAG_SUBMIT). When the submission flag is set, the Mail Submission
  service on Exchange notifies the Transport Service that there are
  messages awaiting to be picked up in the Drafts (Outbox if Outlook).
  If any client reads the message after it’s been flagged for submission
  but before it’s picked up by the Transport service, the MSGFLAG_SUBMIT
  flag is removed which leads to the message being stuck in the Drafts
  folder.</p>
  
  <p>    In order to determine if this is the case, you can sign up for notifications using Streaming Notifications (and a tool the likes of
  <a href="http://ewseditor.codeplex.com/" rel="nofollow noreferrer">EWSEditor</a> or <a href="http://ewsstreaming.codeplex.com/" rel="nofollow noreferrer">EwsStreaming</a>) and check for any item Modified events and
  check for any PR_MESSAGE_FLAGS changes. </p>
  
  <p>    As discussed, the message could have been accessed by any client or API before the transport service went to pick it up, such as OWA,
  Outlook, EWS Managed API, Outlook add-ins or any mobile apps.</p>
</blockquote>

<p>So next task is twofold: </p>

<ol>
<li><p>Identify if anything is reading my messages immediately after they've flagged to be sent, resetting the MSGFLAG_SUBMIT flag. If so, ask them to please stop.</p></li>
<li><p>Include a process to re-send items that I think have been sent that I later find in Drafts. </p></li>
</ol>

<p>I didn't get any information about why I'd get an <code>Access Denied</code> when trying to resend, but perhaps that's consistent with something else modifying the item.</p>

<p>They didn't think there was any chance this is a problem in EWS, where EWS is incorrectly reporting success when actually the message hasn't been flagged to be sent. Which is reassuring.</p>

<p><strong>EDIT (20 June):</strong></p>

<p>Introducing a process to re-send items that are stuck in Drafts didn't work very well. It turns out that sometimes when items are left in Drafts they've actually been sent and received by the recipient, so re-sending them means the recipient gets a duplicate of the email. Or several, if the same problem happens again. Sometimes the item isn't sent but is still removed from Drafts and moved to Sent Items - again it's not possible for the sender to work out whether it's sent or not. Totally weird. I've asked another question here about specifically whether <a href="https://stackoverflow.com/questions/37906379/is-ews-senditem-supposed-to-be-atomic-and-reliable">Exchange message sending is expected to be atomic and reliable</a>. </p>

<hr>

<p>Also posed on <a href="https://social.technet.microsoft.com/Forums/exchange/en-US/7f2da83e-f726-4655-b809-2610dd07dc5d/ews-reports-senditem-as-succeeding-but-message-is-still-in-drafts-folder?forum=exchangesvrdevelopment" rel="nofollow noreferrer">Exchange forums</a></p>