Send emails in NodeJS using SMTP

How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server

send email in javascript

NodeJS provides many functions for sending email using SMTP. One common approach is to use nodemailer in conjunction with an email provider such as MailSlurp.

Setting up

First create a free email account on MailSlurp and get your API Key. Then create a new project and install the following dependencies:

npm init -y
npm install --save nodemailer mailslurp-client debug jest

Create a test to send email

Next we can configure nodemailer in a test using MailSlurp's SMTP server credentials.

const nodemailer = require("nodemailer")
const {MailSlurp, CreateInboxDtoInboxTypeEnum} = require('mailslurp-client');
const log = require('debug')('ms:smtp')

describe('node mailer smtp usage', () => {
let config;

    beforeAll(() => {
        // provide a mailslurp API KEY
        const apiKey = process.env.API_KEY;
        // create config for clients and main class
        config = {apiKey};

    it('can create smtp inbox then send email to it with nodemailer', async () => {
        log("Create inbox")
        const mailslurp = new MailSlurp(config);
        const inbox = await mailslurp.createInbox();

        log("Start transport")
        const transport = nodemailer.createTransport({
            host: "",
            port: 2525,
            secure: false

        const sent = await transport.sendMail({
            from: '"Fred Foo 👻" <>',
            to: inbox.emailAddress,
            subject: "Hello ✔",
            text: "Hello world?",
            html: "<b>Hello world?</b>",
        log("Email sent")

        log("Wait for latest")
        const email = await mailslurp.waitForLatestEmail(, 30000, true)

    it('can create smtp inbox then send email from a mailslurp address', async () => {
        log("Try plain auth")
        const mailslurp = new MailSlurp(config);
        const inbox = await mailslurp.createInboxWithOptions({inboxType:CreateInboxDtoInboxTypeEnum.SMTP_INBOX});
        const server = await mailslurp.getImapSmtpAccessDetails()

        log("Fetched imap smtp access")

        const opts = {
            host: server.smtpServerHost,
            port: server.smtpServerPort,
            secure: false,
            auth: {
                user: server.smtpUsername,
                pass: server.smtpPassword,
                type: "PLAIN"
        log("Create auth plain transport")
        const transport = nodemailer.createTransport(opts)

        log("Send email")
        const sent = await transport.sendMail({
            from: inbox.emailAddress,
            to: inbox.emailAddress,
            subject: "Test outbound email",
            text: "Can I send on behalf?",
            html: "<b>Hello world</b>",

        log("Wait for email to arrive")
        const email = await mailslurp.waitForLatestEmail(, 30000, true)
        expect(email.subject).toContain("Test outbound")

More examples

To see more examples of sending email using NodeJS see the examples page.

Related content

Java email client: send and receive emails and attachments i...

MailSlurp Java SDK for sending and receive email and attachments on the JVM.

Javascript email libraries for MailSlurp

Nodemailer alternatives to send and receieve email in code and tests

SMTP email application libraries

How to send email in CSharp, PHP, Ruby, Java, Python, and NodeJS using the SMTP protocol and MailSlurp mailservers.

Fastest way to start a typescript project

Modern typescript tooling has come a long way. See how to setup a new project with TS-Node, TSC, and typeconfig.json.

Fetch error message javascript

Handle response exceptions with Fetch in Javascript

GraphQL API disposable email accounts

How to create real email addresses using GraphQL to send and receive emails in tests and frontend applications.

Ready to dive in?Start building email applications today.