Skip to content

How to list IMAP email messages

Not every email provider is Google, Microsoft, or Yahoo. Thousands of organizations run their own mail servers, and services like Zoho Mail, Fastmail, AOL, and GMX all support IMAP. Nylas connects to any standard IMAP server and exposes it through the same Messages API you’d use for Gmail or Outlook.

This guide covers listing messages from generic IMAP accounts, the catch-all provider type for anything that isn’t Google, Microsoft, Yahoo, or iCloud.

Building a production-grade IMAP integration is a bigger project than most developers expect. The protocol requires persistent socket connections with heartbeat monitoring and reconnection logic. Messages come back in MIME format, which means parsing multipart content, handling character encodings, and extracting inline attachments. You need to track message UIDs per folder, handle UIDVALIDITY changes that can invalidate your entire cache, and deal with server-specific quirks like different hierarchy separators and inconsistent folder naming.

Nylas does all of that behind a REST API. You get clean JSON responses, automatic sync with a local cache, and a single integration that works across IMAP, Gmail, Outlook, Yahoo, and iCloud. Sending email requires a separate SMTP connection in raw IMAP, but Nylas handles both protocols behind one API.

If you’re building a quick integration with a single IMAP server and want full control, you can connect directly. For production apps that need reliability across multiple providers, Nylas saves you months of infrastructure work.

You’ll need:

  • A Nylas application with a valid API key
  • A grant for an IMAP email account
  • The IMAP server hostname and port for the provider (e.g., imap.example.com:993)

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

The generic IMAP connector works with any standard IMAP server. Nylas supports two authentication flows:

MethodBest for
Hosted OAuthProduction apps where Nylas collects IMAP credentials through a guided flow
Bring Your Own (BYO) AuthenticationCustom auth pages where you collect IMAP host, port, and credentials directly

With Hosted OAuth, users enter their email credentials and Nylas automatically connects. If the IMAP server requires specific connection settings, users can expand “Additional settings” to enter the IMAP host, port, SMTP host, and SMTP port manually.

With BYO Authentication, your app collects the credentials and sends them to Nylas directly:

SettingDescriptionExample
imap_usernameEmail address or username[email protected]
imap_passwordPassword or app password(app-specific password)
imap_hostIMAP server hostnameimap.example.com
imap_portIMAP server port993
smtp_hostSMTP server hostnamesmtp.example.com
smtp_portSMTP server port465

Most IMAP providers require app passwords instead of the user’s regular login password. This is especially true for providers with two-factor authentication enabled. See the app passwords guide for provider-specific instructions.

If your app needs to send email (not just read), add options=smtp_required to the Hosted OAuth URL. This ensures users enter their SMTP server details during authentication.

The full setup walkthrough is in the IMAP authentication guide.

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 IMAP 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:

IMAP providers support the search_query_native parameter, which maps to the IMAP SEARCH command defined in RFC 3501. Like Yahoo and iCloud, generic IMAP lets you combine search_query_native with any other query parameter.

Not all IMAP servers support the SEARCH operator. If search_query_native returns a 400 error, the provider doesn’t support it. Fall back to standard query parameters (subject, from, to, etc.) instead.

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

The generic IMAP connector is the most flexible provider type. It works with nearly any mail server, but that flexibility comes with some trade-offs you should understand.

Nylas maintains a rolling cache of messages from the last 90 days for all IMAP-based providers. Anything received or created within that window is synced and available through the API. For messages older than 90 days, set query_imap=true to query the IMAP server directly. This is slower because of provider latency, but it reaches the full mailbox.

When using query_imap, you must include the in parameter to specify which folder to search.

Nylas webhooks only fire for changes to messages within the 90-day cache window. If a user modifies or deletes a message older than 90 days, you won’t receive a notification. Plan your sync strategy accordingly if your app needs to track changes across the full mailbox.

Unlike Google (labels) or Microsoft (standardized internal names), IMAP folder names are set by each provider. What shows up as “Trash” in one provider’s web UI might be “Deleted Messages” or “Deleted Items” on the IMAP server.

Nylas maps common folders using RFC 9051 standard attributes (\Inbox, \Sent, \Drafts, \Trash, \Junk, \Archive), but always use the List Folders endpoint to discover the actual folder names for each account.

IMAP servers also use different hierarchy separators. Some use . (like INBOX.Accounting.Taxes) and others use / or \. Nylas returns the full folder path as-is, so a nested folder might appear as Accounting.Taxes or INBOX\Accounting\Taxes depending on the server.

Folders may take up to 10 minutes to appear after authentication. If a newly connected account shows no folders, wait a few minutes for the initial sync to complete.

Each IMAP provider sets its own rate limits, and most don’t publish them. If your app hits a rate limit, Nylas handles the retry automatically. But if you’re polling aggressively for many IMAP users, you may see throttling.

Use webhooks instead of polling to avoid rate limit issues. Let Nylas notify you of changes instead of checking repeatedly.

IMAP servers use a value called UIDVALIDITY to track whether message UIDs in a folder are still valid. If the server changes this value (due to a folder rebuild, migration, or misconfiguration), Nylas re-indexes the entire folder to stay in sync.

This usually happens transparently, but it can cause:

  • Temporary inconsistency where the API may return stale or incomplete results for that folder during re-indexing
  • Sync failures with misconfigured servers where the provider returns a different UIDVALIDITY on every connection, preventing Nylas from maintaining a stable cache. You’ll see an error: Stopped due to too many UIDVALIDITY resyncs

If you encounter UIDVALIDITY errors, check the IMAP troubleshooting guide for workarounds.

IMAP messages must be:

  • UTF-8 or ASCII encoded. Messages with other character encodings may not parse correctly.
  • RFC 5322 compliant. They must conform to the Internet Message Format standard.
  • Include a Message-ID header. Nylas uses this to identify individual messages.

Most modern mail servers enforce these requirements, but custom or legacy servers may not. If messages aren’t syncing, encoding is a common cause.

Nylas maintains two low-bandwidth IMAP idle connections per account to monitor the Inbox and Sent folders for real-time changes. Other folders are checked periodically. This means:

  • Inbox and Sent changes are detected quickly, typically within seconds
  • Other folders may take a few minutes to reflect changes
  • Webhook latency varies by folder. Expect faster notifications for Inbox activity than for custom folders.

If a provider doesn’t support IMAP idle, Nylas falls back to periodic polling, which increases detection time.

The generic IMAP connector provides email access only. If you need calendar functionality alongside email for a provider that supports CalDAV (like iCloud or Fastmail), check whether Nylas has a dedicated connector for that provider.

The Contacts API is disabled by default for IMAP grants. When enabled (via Nylas Support), contacts are parsed from email headers (From, To, CC, BCC, Reply-To fields) rather than synced from an address book.

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.