# Get emails from code (how to receive emails programmatically)

Once you have created an inbox any emails sent to its email address received and saved under the inboxes ID. MailSlurp allows you to fetch and receive emails send to an inbox's email address in several ways:

# Options

# Visual dashboard

The simplest way to receive emails is to create an inbox and send it an email using your email provider. You can then view the emails in your dashboard.

# Indiviual emails

If you know that an email exists and you have its id simply call a getEmail(emailId) method to get a full email object that includes the body.

// getEmail(emailId) -> returns a full email object
  "id": "123",
  "from": "",
  "to": "",
  "subject": "",
  "body": "<h1>hello</h1>",
  "html": true,
  "attachments": []
  // ...other fields hidden

# WaitFor methods

For programmatic use-cases the MailSlurp API and SDKs provide WaitFor methods. This is the recommended way to receive emails.

WaitFor methods hold a connection until a condition is met. If the condition is met a list of matching EmailPreview entities is returned. These contain email id values that can be used to fetch individual emails.

There are several waitFor variations:

// typescript used for example. methods also available in all SDKs and REST API
 * Call an inbox and return the last received email
 * OR wait until an email is received or timeout is reached
waitForLatestEmail(inboxId?: string, timeout?: number): Promise<Email>
 * Hold connection until inbox contains at least
 * expected number of emails and then return them
waitForEmailCount(count?: number, inboxId?: string, timeout?: number): Promise<EmailPreview[]>
 * Hold connection until inbox contains at least
 * expected number of emails that match a given match option input and return them
waitForMatchingEmails(matchOptions: MatchOptions, count?: number, inboxId?: string, timeout?: number): Promise<EmailPreview[]>
 *  Call an inbox and return email number `n` or wait for it to be received
waitForNthEmail(inboxId: string, index: number, timeout?: number): Promise<Email>

# All Emails

You can fetch all existing emails from an inbox with the getEmails methods. These methods return a collection of List<EmailPreview>.

// getEmails(inboxId) -> returns collection
    "id": "123"
    "id": "456"

To get the body or attachments for an email you must call getEmail or getAttachment methods individually.

# Webhooks

WebHooks are a way to be notified via HTTP POST when a message is received. If you attach a WebHook to an inbox the WebHook URL will be notified when a new email is received. WebHooks are useful for high throughput applications or for environments where WaitFor methods are not appropriate.

# Receiving attachments

Whenever MailSlurp receives email it extras attachments and stores them securely to disk. Attachments are not returned directly when emails are fetched. Instead you must obtain the attachment IDs from an email and then call attachment methods to receive attachment metadata and binary content.

// get attachment content as HTTP OctetStream
downloadAttachment(emailId: string, attachmentId: string): Promise<Response>
interface AttachmentMetaData {
  contentLength: number;
  contentType: string;
  name: string;

Next →