title: Download attachment from Exchange using Exchange Web Services
body: <p>I am trying to use the following code to connect and download an attachment from email in an inbox using C# and Exchange Web Services but I am getting a 'System.ArgumentOutOfRangeException' error and I cant see why.  I have googled for an answer but i cant find one or the answers I find are for very old versions of EWS.</p>
  
  <p>I know that the rest of the code generally works as I can access other information relating to the email, just not access the attachment.</p>
  
  <p>Cany anyone show me the error of my ways?</p>
  
  <p>Thanks in advance,</p>
  
  <p>James</p>
  
  <pre><code>    static void Main(string[] args)
      {
          ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
          service.Credentials = new NetworkCredential("MYLOGIN", "MYPASSWORD", "MYDOMAIN");
  
          service.Url = new Uri("https://MYMAILSERVER/EWS/Exchange.asmx");
  
          ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  
          FindItemsResults&lt;Item&gt; findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(1000));
  
          foreach (Item item in findResults.Items)
          {
              if (item.HasAttachments &amp;&amp; item.Attachments[0] is FileAttachment)
              {
                  FileAttachment fileAttachment = item.Attachments[0] as FileAttachment;
                  fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
              }
  
          }
      }
  }
  </code></pre>
  
  <p><strong>Solved but new problem</strong></p>
  
  <p>I have sorted the issue now by changing the 'foreach (Item item in findResults.Items)' to 'foreach (EmailMessage item in findResults.Items)' but now I need to find out how to enumerate through the attachments - any ideas anyone?</p>