Send emails in NodeJS using SMTP

How to use Javascript SMTP client (or Nodemailer) to send email with MailSlurp mail server. Read and download mail using IMAP with Node

  • Table of contents

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;
        expect(apiKey).toBeTruthy();
        // create config for clients and main class
        config = {apiKey};
    });

    it('can create [smtp inbox](/guides/csharp-send-email-smtp/) then send email to it with nodemailer', async () => {
        log("Create inbox")
        const mailslurp = new MailSlurp(config);
        const inbox = await mailslurp.createInbox();
        expect(inbox.id).toBeTruthy();
        expect(inbox.emailAddress).toContain('@mailslurp.com');

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

        const sent = await transport.sendMail({
            from: '"Fred Foo 👻" <foo@example.com>',
            to: inbox.emailAddress,
            subject: "Hello ✔",
            text: "Hello world?",
            html: "<b>Hello world?</b>",
        });
        log("Email sent")
        expect(sent.messageId).toBeTruthy()

        log("Wait for latest")
        const email = await mailslurp.waitForLatestEmail(inbox.id, 30000, true)
        expect(email.subject).toContain("Hello")
        expect(email.isHTML).toBeTruthy()
    });


    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")
        expect(inbox.id).toBeTruthy();
        expect(inbox.emailAddress).toContain('@mailslurp.mx');

        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>",
        });
        expect(sent.messageId).toBeTruthy()

        log("Wait for email to arrive")
        const email = await mailslurp.waitForLatestEmail(inbox.id, 30000, true)
        expect(email.subject).toContain("Test outbound")
        expect(email.isHTML).toBeTruthy()
    });
});

More examples

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

Email and SMS Platform
Create a free account in 3 clicks