Skip to main content

IMAP SMTP inbox access

MailSlurp mailboxes provide IMAP and SMTP access to your email accounts. This allows you to control inboxes using your favourite email client or using SMTP/IMAP libraries in any language you choose.

Access endpoints

MailSlurp access points for many client configurations.

ProtocolHostPortTLSDescription
SMTPmx.mailslurp.com25falseSMTP server
SMTPmx.mailslurp.com2525falseSMTP server
SMTPmailslurp.mx587trueSMTP server (secure)
SMTPmailslurp.mx2587trueSMTP server (secure)
SMTPmailslurp.mx465trueSMTP server (secure)
SMTPmailslurp.mx2465trueSMTP server (secure)
IMAPmailslurp.click1143falseIMAP server
HTTP(S)api.mailslurp.com80trueREST API server

Creating an SMTP inbox

To access an inbox with SMTP or IMAP you must create a new inbox using the SMTP_INBOX mailbox type. Once created you will see a username and password for each protocol in the dashboard. You can also obtain access credentials using the API.

import {CreateInboxDtoInboxTypeEnum, MailSlurp} from 'mailslurp-client';
const apiKey = process.env.API_KEY;
const mailslurp = new MailSlurp({ apiKey });

// create an smtp inbox
const inboxSmtp = await mailslurp.inboxController.createInboxWithOptions({
createInboxDto: {
inboxType: CreateInboxDtoInboxTypeEnum.SMTP_INBOX,
}
});

Get access credentials

Each mailbox has a unique username and password for connection with the SMTP and IMAP servers. Use the getImapSmtpAccessDetails method to list access credentials:

// get access details
const {
secureSmtpServerHost,
secureSmtpServerPort,
smtpServerHost,
smtpServerPort,
smtpUsername,
smtpPassword,
imapUsername,
imapPassword,
imapServerHost,
imapServerPort,
} = await mailslurp.getImapSmtpAccessDetails(inboxSmtp.id)

Connect using SMTP client in code

To control a mailbox in code using an SMTP library use the credentials, host and port to configure your library. Here is an example in NodeJS using the popular nodemailer package.

import nodemailer from "nodemailer";

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

Sending emails with SMTP

SMTP is for sending emails. You must use IMAP or the MailSlurp API methods to read emails.

Sending email to a MailSlurp inbox using SMTP

You can send emails to a MailSlurp inbox without any credentials like so:

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>",
});

Send emails from an inbox

To send emails from a MailSlurp inbox you must use the credentails associated with it to verify your ownership. Pass the SMTP auth details using PLAIN method.

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

// 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>",
});

Full examples

See the examples repository for more uses.

Loading...