MailSlurp NodeMailer SMTP Usage

Use SMTP nodemailer with MailSlurp disposable email addresses. Configure SMTP client access to mailslurp inboxes.

  • Table of contents

You can use MailSlurp's unlimited throw away email accounts to send and receive email in NodeJS using Nodemailer. Here is an example using both clients:

const nodemailer = require("nodemailer")
const {MailSlurp} = require('mailslurp-client');

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 then send email to it with nodemailer', async () => {
        const mailslurp = new MailSlurp(config);
        const inbox = await mailslurp.createInbox();
        expect(inbox.id).toBeTruthy();
        expect(inbox.emailAddress).toContain('@mailslurp.com');

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

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