MailSlurp logo

guides

Fetch Email Messages and Attachments in Code

Capture and read inbound emails with MailSlurp in tests and applications. Learn message listing, content retrieval, attachments, and webhook-ready receive patterns.

Use with your tools
Fetch Email Messages and Attachments in Code article preview

This guide explains how to read inbound email content and attachments once messages have reached a MailSlurp inbox.

For advanced receive orchestration (waitFor, webhook strategy, and CI patterns), see receive emails in code.

Step 1: capture inbound email

Create or select an inbox, then send a message to its address. MailSlurp stores inbound messages under that inbox ID.

Step 2: list messages

Use inbox listing methods to retrieve message previews, then fetch full content by email ID.

Message previews are good for selecting candidates; full message fetch is required for complete body/header analysis.

Step 3: inspect content

In dashboard and API workflows, you can inspect:

  • Envelope and headers (to, from, subject)
  • Plain text body
  • HTML body
  • Raw source for low-level analysis

Dashboard examples:

email overview

email text content

Step 4: retrieve attachments

Attachments are stored separately and retrieved by attachment ID.

// download binary attachment content
downloadAttachment(emailId: string, attachmentId: string): Promise<Response>
interface AttachmentMetaData {
  contentLength: number;
  contentType: string;
  name: string;
}

Dashboard attachment view:

receive attachments

Step 5: verify deliverability quality

Use spam and authentication diagnostics when message quality is part of the test objective:

Dashboard analysis example:

email analysis

Practical receive checklist

  • Use a dedicated inbox per test or environment.
  • Add timeout-aware receive assertions.
  • Validate links/codes and key headers.
  • Download and verify attachment MIME types.
  • Track failures via webhooks for async systems.

Next step

Continue with send emails in code or build end-to-end assertions in Cypress.