If you need your application or AI agent to receive email programmatically, you have three approaches. Each works, but each has significant trade-offs.

Option 1: IMAP

IMAP (Internet Message Access Protocol) is the traditional way applications access email. Libraries like imaplib (Python), node-imap (Node.js), or javax.mail (Java) connect to an email server and retrieve messages.

Pros: Works with any existing email account (Gmail, Outlook, etc.)

Cons: Complex API. Google and Microsoft now require OAuth2 instead of passwords, making setup much harder. Rate limits apply. You must poll repeatedly. No push notifications. Shared inboxes cause coordination problems for concurrent systems.

Option 2: REST API Polling

# Poll every 5 seconds until an email arrives
while True:
    r = requests.get(f"{BASE}/v1/inboxes/{inbox_id}/emails", headers=headers)
    emails = r.json()["data"]
    if emails:
        process(emails[0])
        break
    time.sleep(5)

Pros: Simpler than IMAP. Works anywhere. No OAuth setup.

Cons: Average 2.5-second delay per poll interval. Burns API quota. Keeps a process running indefinitely.

Option 3: Long-Poll API (Recommended)

# One call that blocks until email arrives
r = requests.get(
    f"{BASE}/v1/inboxes/{inbox_id}/wait?timeout=60",
    headers=headers,
    timeout=65
)
email = r.json()

Pros: Instant response when email arrives. One HTTP call instead of repeated polling. No wasted quota. Simple to use.

Cons: Connection stays open for up to the timeout duration.

Option 4: Webhooks

Configure a webhook URL and AgentMailr POSTs to it the moment an email arrives.

Pros: Truly push-based. Scales to any volume. No connection held open.

Cons: Requires a public HTTPS endpoint. More complex to set up for local development or simple scripts.

Recommendation

For scripts and agent workflows: use long-poll. For production servers with many agents: use webhooks. Avoid IMAP unless you must read from a pre-existing email account.