title: How to SendAndSave email with EWS with windows auth and identity without a mailbox?
body: <p>My scenario is this: </p>
  
  <ul>
  <li>I connect to Exchange Web Services (EWS) using a service account and windows authentication</li>
  <li>I don't use Impersonation</li>
  <li>The service account doesn't have a mailbox</li>
  <li>I connect to a mailbox 'MyMailbox' to read and send emails. Whenever I send emails I use <code>SendAndSaveCopy()</code>, specifying MyMailbox's SentItems folder to save in. Without specifying the folder <code>SendAndSaveCopy()</code> fails, understandably, as there's no mailbox linked to my service account where it could save the email. </li>
  <li>I sometimes want to Send As or Send On Behalf Of another user, so I set the <code>From</code> property to that other user's primary SMTP address for that. </li>
  </ul>
  
  <p>In this scenario things work fine until I send an email with an attachment. In that case it fails with <code>ResponseCode = ErrorMissingEmailAddress</code> and <code>MessageText = When making a request as an account that does not have a mailbox, you must specify the mailbox primary SMTP address for any distinguished folder Ids.</code></p>
  
  <p>Looking at the SOAP message that's sent from my <code>SendAndSaveCopy()</code> I can see it's quite different when I have an attachment - note the <code>MessageDisposition="SaveOnly"</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="Exchange2010_SP2" /&gt;
      &lt;/soap:Header&gt;
      &lt;soap:Body&gt;
        &lt;m:CreateItem MessageDisposition="SaveOnly"&gt;
          &lt;m:Items&gt;
            &lt;t:Message&gt;
              &lt;t:Subject&gt;My test email subject&lt;/t:Subject&gt;
              &lt;t:Body BodyType="HTML"&gt;....
  </code></pre>
  
  <p>whereas when sending without an attachment it has <code>&lt;m:CreateItem MessageDisposition="SendAndSaveCopy"&gt;</code>. It seems reasonable that EWS is first saving the email and then it'll send it. Indeed the <a href="https://msdn.microsoft.com/en-us/library/office/dd634248%28v=exchg.80%29.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow">API doc</a> says </p>
  
  <blockquote>
    <p>This method does not work if the message has unsaved attachments. In that case, the message must first be saved and then sent. </p>
  </blockquote>
  
  <p>Although <code>SendAndSaveCopy()</code> seems to work fine when I'm connecting as an identity that has a mailbox, and <a href="https://msdn.microsoft.com/en-us/library/office/hh532564(v=exchg.80).aspx" rel="nofollow">MSDN example code</a> shows calling <code>SendAndSaveCopy()</code> without any interim steps. The example specifically says that <code>SendAndSaveCopy()</code> results in three calls to EWS: CreateItem, CreateAttachment, SendItem. </p>
  
  <p>My main question is: how should I call EWS to achieve the same thing as <code>SendAndSaveCopy()</code> but when I have an attachment? </p>
  
  <p>A secondary question: is this a bug? Shouldn't <code>SendAndSaveCopy()</code> when given a folderId use that folder for the CreateItem call? </p>
  
  <p>I'm using EWS 15.0.0.0 and specifying Exchange2010_SP2 api version. </p>
  
  <p>UPDATE:</p>
  
  <p>On further inspection I see that EWS Managed API is also issuing multiple EWS calls for <code>SendAndSaveCopy()</code> when there are attachments and I'm using username &amp; password auth. I see two EWS messages: </p>
  
  <ul>
  <li>The first has <code>&lt;m:CreateItem MessageDisposition="SaveOnly"&gt;</code> and notably <em>no</em> <code>SavedItemFolderId</code>. So where is it saved!? My guess is it's saved to a default location for the current user's mailbox, which fails for my windows auth scenario because there's no current user's mailbox. </li>
  <li>The response from this provides the ItemId for the saved mail message and its saved attachment.</li>
  <li>The second EWS call has <code>&lt;m:SendItem SaveItemToFolder="true"&gt;</code> and also sets the <code>SavedItemFolderId</code></li>
  </ul>
  
  <p>I still feel like this is a bug or missing feature from <code>SendAndSaveCopy()</code>: it should use the specified folder location to save the temp message. But in any case, what EWS Managed API calls <em>should</em> I use to get the desired effect? </p>