Skip to content

How to list Exchange email messages

Exchange on-premises servers are still common in enterprise environments. If your users run self-hosted Exchange (2007 or later), Nylas connects to them through Exchange Web Services (EWS), a separate protocol from the Microsoft Graph API used for Exchange Online and Microsoft 365.

The same Messages API you use for Gmail and Outlook works for Exchange on-prem accounts. This guide covers the EWS-specific details: when to use EWS vs. Microsoft Graph, authentication, message ID behavior, and search capabilities.

This is the first thing to figure out. The two provider types target different Exchange deployments:

Provider typeConnectorUse when
Microsoft GraphmicrosoftExchange Online, Microsoft 365, Office 365, Outlook.com, personal Microsoft accounts
Exchange Web ServicesewsSelf-hosted Exchange servers (on-premises)

If the user’s mailbox is hosted by Microsoft in the cloud, use the Microsoft guide instead. The ews connector is specifically for organizations that run their own Exchange servers.

Microsoft announced EWS retirement and recommends migrating to Microsoft Graph. However, many organizations still run on-premises Exchange servers where EWS is the only option. Nylas continues to support EWS for these environments.

EWS is a SOAP-based XML API. Every request requires building XML SOAP envelopes, every response needs XML parsing, and errors come back as SOAP faults with nested XML structures. You need to handle autodiscovery to find the right server endpoint (which is frequently misconfigured in enterprise environments), manage authentication with support for two-factor app passwords, and work with EWS-specific data formats that don’t match any other email provider.

Nylas replaces all of that with a JSON REST API. No XML, no WSDL, no SOAP. Authentication and autodiscovery are handled automatically. Your code stays the same whether you’re reading from Exchange on-prem, Exchange Online, Gmail, or any IMAP provider.

If you have deep EWS experience and only target Exchange on-prem, you can integrate directly. For multi-provider support or faster time-to-integration, Nylas is the simpler path.

You’ll need:

  • A Nylas application with a valid API key
  • A grant for an Exchange on-premises account
  • An EWS connector configured with the appropriate scopes
  • The Exchange server accessible from outside the corporate network (not behind a VPN or firewall that blocks external access)

New to Nylas? Start with the quickstart guide to set up your app and connect a test account before continuing here.

Create an EWS connector with the scopes your app needs:

ScopeAccess
ews.messagesEmail API (messages, drafts, folders)
ews.calendarsCalendar API
ews.contactsContacts API

During authentication, users sign in with their Exchange credentials, typically the same username and password they use for Windows login. The username format is usually [email protected] or DOMAIN\username.

If EWS autodiscovery is configured on the server, authentication works automatically. If not, users can click “Additional settings” and manually enter the Exchange server address (e.g., mail.company.com).

Users with two-factor authentication must generate an app password instead of using their regular password. See Microsoft’s app password documentation for instructions.

The full setup walkthrough is in the Exchange on-premises provider guide.

The Exchange server must be accessible from Nylas’s infrastructure:

  • EWS must be enabled on the server and exposed outside the corporate network
  • If the server is behind a firewall, you’ll need to allow Nylas’s IP addresses (available on contract plans with static IPs)
  • If EWS isn’t enabled, Nylas can fall back to IMAP for email-only access, but you lose calendar and contacts support

Accounts in admin groups are not supported.

Make a List Messages request with the grant ID. By default, Nylas returns the 50 most recent messages. These examples limit results to 5:

You can narrow results with query parameters. Here’s what works with Exchange accounts:

ParameterWhat it doesExample
subjectMatch on subject line?subject=Weekly standup
fromFilter by sender[email protected]
toFilter by recipient[email protected]
unreadUnread messages only?unread=true
inFilter by folder or label ID?in=INBOX
received_afterAfter a Unix timestamp?received_after=1706000000
received_beforeBefore a Unix timestamp?received_before=1706100000
has_attachmentOnly messages with attachments?has_attachment=true

Here’s how to combine filters. This pulls unread messages from a specific sender:

Exchange supports the search_query_native parameter using Microsoft’s Advanced Query Syntax (AQS). You can combine search_query_native with any query parameter except thread_id.

AQS queries must be URL-encoded. For example, subject:invoice becomes subject%3Ainvoice in the URL. The SDKs handle this automatically, but you’ll need to encode manually in curl requests.

Exchange doesn’t support searching by BCC field. If you include BCC in a search_query_native query, results may be incomplete or return an error.

The Exchange server must have the AQS parser enabled and search indexing active for search_query_native to work. If queries aren’t returning expected results, the Exchange admin should verify these settings.

See the search best practices guide for more on search_query_native across providers.

Exchange on-prem behaves differently from Exchange Online (Microsoft Graph) in several important ways.

This is the most important Exchange-specific behavior. When a message is moved from one folder to another, its Nylas message ID changes. This is expected EWS behavior because Exchange assigns new IDs when messages change folders.

If your app stores message IDs, treat them as folder-specific pointers, not permanent identifiers. For tracking a message across folder moves, use the InternetMessageId header instead. It stays stable regardless of which folder the message is in.

To get the InternetMessageId, include the fields=include_headers query parameter:

Multiple messages can share the same InternetMessageId in Exchange. This happens when copies of a message exist in multiple folders. Use it for correlation, not as a unique key.

Exchange supports nested folders. Nylas returns a flat folder list but includes a parent_id field on child folders so you can reconstruct the hierarchy. Use parent_id when creating or updating folders to place them in the right location.

Use the List Folders endpoint to discover all folders and their hierarchy for an Exchange account.

The starred query parameter only works on Exchange 2010 and later. If you’re targeting Exchange 2007, filtering by starred status isn’t available.

Unlike Google and Microsoft’s cloud services, Exchange on-prem rate limits are set by the server administrator. Nylas can’t predict what they’ll be. If the Exchange server throttles a request, Nylas returns a Retry-After header with the number of seconds to wait.

For apps that check mailboxes frequently, webhooks are the best way to avoid hitting rate limits. Let Nylas notify you of changes instead of polling.

Exchange processes received_before and received_after filters at the day level, not second-level. Even though Nylas accepts Unix timestamps, Exchange rounds to the nearest day. Results are inclusive of the specified day.

For example, if you filter with received_after=1706745600 (February 1, 2024 00:00:00 UTC), you’ll get all messages from February 1 onward, including messages received earlier that day.

Exchange relies on a search index for queries using to, from, cc, bcc, and any_email. If a message has just arrived but the search index hasn’t refreshed yet, it won’t appear in filtered results. The search index refresh interval is controlled by the Exchange administrator.

If filtered queries aren’t returning recently received messages, this is likely the cause. Unfiltered list requests (no query parameters) always return the latest messages.

If EWS isn’t enabled on the Exchange server, Nylas can still connect via IMAP for email-only access. This means:

  • Email works with messages, drafts, and folders available
  • Calendar and contacts are not available because these require EWS
  • The 90-day message cache applies because IMAP connections use the same caching behavior as other IMAP providers, with query_imap=true for older messages

If your users report that calendar or contacts aren’t working, verify that EWS is enabled on their Exchange server.

The Messages API returns paginated responses. When there are more results, the response includes a next_cursor value. Pass it back as page_token to get the next page:

Keep paginating until the response comes back without a next_cursor.