title: Fetch emails received on certain day from non-Well-known folder in shared inbox, Exchange 2016 EWS in C#
body: <p>I'm trying to fetch all messages from a user-specified date on an Exchange 2016 server using the EWS managed API in C#.</p>
  
  <p>I authenticate with:</p>
  
  <pre><code>public static void Login(string username, string password)
      {
          service.UseDefaultCredentials = false;
          service.Credentials = new WebCredentials(username, password);
          service.AutodiscoverUrl(username, RedirectionUrlValidationCallback);
      }
  </code></pre>
  
  <p>Then select the appropriate inbox with </p>
  
  <pre><code>sharedMailbox = new Mailbox(Properties.Settings.Default.Inbox);
  </code></pre>
  
  <p>the SMTP address is stored in Settings.settings .</p>
  
  <p>I then find the desired folder using the following (from <a href="https://stackoverflow.com/questions/7912584/exchange-web-service-folderid-for-a-not-well-known-folder-name">this thread</a>):</p>
  
  <pre><code>        targetFolderId = new FolderId(WellKnownFolderName.Inbox, sharedMailbox);
          // set folder view
  
          view.PropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
          view.PropertySet.Add(FolderSchema.DisplayName);
          view.Traversal = FolderTraversal.Deep;
          folderResults = service.FindFolders(WellKnownFolderName.Inbox, view);
          foreach(Folder f in folderResults)
          {
              if(f.DisplayName == "Invoices")
              {
                  targetFolderId = f.Id;
                  //tried showing a message box here
              }
          }
  </code></pre>
  
  <p>And use the following (filter code from <a href="https://stackoverflow.com/questions/15557485/exchange-ews-managed-api-2-0-get-by-date">here</a> and retrieve details from Exchange server code from <a href="https://stackoverflow.com/questions/20662855/fetching-all-mails-in-inbox-from-exchange-web-services-managed-api-and-storing-t">here</a>) to get the messages I want:</p>
  
  <pre><code>public static void FetchUnreadMessages(DateTime searchDate)
      {
  
          SearchFilter greaterthanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, searchDate);
          SearchFilter lessthanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, searchDate.AddDays(1));
          SearchFilter dayFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterthanfilter, lessthanfilter);
  
  
  
  
          results = service.FindItems(targetFolderId, dayFilter, view);
          foreach(var item in results.Items)
          {
              emails.Add((EmailMessage)item);
          }
  
  
  
          PropertySet properties = (BasePropertySet.FirstClassProperties);
          service.LoadPropertiesForItems(emails, properties);
  
  
  
      }
  </code></pre>
  
  <p>I'm not sure where this is breaking down. I tried showing a message box in the foreach loop that finds the folder with the specified name, and it appears the folder is never found. I know there is a folder with that display name in the shared inbox.</p>
  
  <p>I'm not great at debugging and unfortunately my grasp of the EWS API is pretty shaky. Any suggestions as to what I'm missing are welcome.</p>
  
  <p>I keep everything related to the inbox in a static class so I only have to worry about one instance. </p>