Receive email in code

Use MailSlurp email APIs to fetch and process emails in tests or apps.

How to receive emails in tests and event listeners

Many applications today have the need to either send or receive emails. Perhaps your app needs to receive invoices from customers via email; or you want to test user sign-up processes with real email addresses. In each instance you need to somehow get SMTP mesages into memory from within a test or applicaiton. You also want these emails to arrive as soon as possible and without flaky polling strategies.

Webhooks

For applications An SMTP queue system with push notifications would be ideal. That way our server can save unnecessary HTTP polling and batching and instead react upon each inbound email directly. MailSlurp SMTP WebHooks offer a solution.

WaitFor polling

For tests (that can afford to block CPU while waiting for a specific email) a simple HTTP call that holds a connection until an email of interest is found or timesout would suffice.

Email APIs

Luckily both options are available using email APIs like MailSlurp. Email APIs let you send and receive emails outside of the SMTP protocols. They provide an abstraction layer over the standard email messaging protocol.

MailSlurp is an email API. It was originally designing specifically for receiving emails in tests and applications and later expanded to include other features like sending of attachments and email account generation.

How to receive emails with MailSlurp

Receiving emails with MailSlurp is easy. Install an official SDK library to your project (or use the REST API). Create an email address using the createNewInbox method. Decide if you want to use a WebHook or waitFor method.

  • Webhooks are for receiving an email directly to an HTTP endpoint
  • waitFor methods are HTTP calls that hold the connection until a desired email is received and returned

The emails received are in a JSON format that looks something like this:

{
  "to": "xks82lf@mailslurp.com",
  "subject": "Invoice for May",
  "from": "joeblogs@gmail.com",
  "body": "...",
  "attachments": []
}

Get started

MailSlurp is free for personal use and has support for receiving emails in Csharp, Java, Node, Python, PHP, Ruby and more. Check it out at mailslurp.com.