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.
Protocol | Host | Port | TLS | Description |
---|---|---|---|---|
SMTP | mailslurp.mx | 25 | true | SMTP server |
SMTP | mailslurp.mx | 587 | true | SMTP server |
SMTP | mailslurp.mx | 2587 | true | SMTP server |
SMTP | mailslurp.mx | 465 | true | SMTP server |
SMTP | mailslurp.mx | 2465 | true | SMTP server |
SMTP | mx.mailslurp.com | 2525 | false | SMTP server (insecure) |
IMAP | mailslurp.click | 1143 | false | IMAP server |
HTTP(S) | api.mailslurp.com | 80 | true | REST API server |
StartTLS is enabled by default.
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.
@Test
public void canSendWithSmtp() throws MessagingException, ApiException {
CreateInboxDto opts = new CreateInboxDto();
opts.setInboxType(CreateInboxDto.InboxTypeEnum.SMTP_INBOX);
InboxControllerApi inboxControllerApi = new InboxControllerApi(apiClient);
InboxDto inbox = inboxControllerApi.createInboxWithOptions(opts);
ImapSmtpAccessDetails server = inboxControllerApi.getImapSmtpAccess(inbox.getId());
Properties prop = new Properties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.starttls.enable", "false");
prop.put("mail.smtp.host", server.getSmtpServerHost());
prop.put("mail.smtp.port", server.getSmtpServerPort());
class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = server.getSmtpUsername();
String password = server.getSmtpPassword();
return new PasswordAuthentication(username, password);
}
}
Authenticator auth = new SMTPAuthenticator();
Session mailSession = Session.getDefaultInstance(prop, auth);
mailSession.setDebug(true);
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(inbox.getEmailAddress()));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(inbox.getEmailAddress()));
message.setSubject("Test subject");
String msg = "This is my first email";
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(msg, "text/html; charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
Transport.send(message);
}
// create inbox
$create = new \MailSlurp\Models\CreateInboxDto(["inbox_type"=>"SMTP_INBOX"]);
$inbox = $inboxController->createInboxWithOptions($create);
$this->assertTrue($inbox->getInboxType() == "SMTP_INBOX");
// create smtp
$server = $inboxController->getImapSmtpAccess();
$mail = new PHPMailer(true);
try {
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Host = $server->getSmtpServerHost();
$mail->Port = $server->getSmtpServerPort();
$mail->SMTPAutoTLS = false;
$mail->SMTPAuth = false;
$mail->setFrom("test@gmail.com", 'Test1');
$mail->addAddress($inbox->getEmailAddress(), 'Test2');
$mail->isHTML(true);
$mail->Subject = 'Test subject';
$mail->Body = 'This is test HTML body <b>in bold!</b>';
$mail->addAttachment($this->pathToAttachment2);
// now connect and send
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
if ($mail->ErrorInfo) {
echo $mail->ErrorInfo;
}
$this->assertTrue($mail->ErrorInfo == '');
with mailslurp_client.ApiClient(configuration) as api_client:
inbox_controller = mailslurp_client.InboxControllerApi(api_client)
inbox1 = inbox_controller.create_inbox_with_options(CreateInboxDto(inbox_type="SMTP_INBOX"))
inbox2 = inbox_controller.create_inbox()
assert "@mailslurp.mx" in inbox1.email_address
# get smtp imap access
smtp_access = inbox_controller.get_imap_smtp_access(inbox_id=inbox1.id)
msg = "Subject: Test subject\r\n\r\nThis is the body"
with SMTP(host=smtp_access.smtp_server_host, port=smtp_access.smtp_server_port) as smtp:
smtp.login(user=smtp_access.smtp_username, password=smtp_access.smtp_password)
smtp.sendmail(from_addr=inbox1.email_address, to_addrs=[inbox2.email_address], msg=msg)
smtp.quit()
wait_for_controller = mailslurp_client.WaitForControllerApi(api_client)
email = wait_for_controller.wait_for_latest_email(inbox_id=inbox2.id)
assert "Test subject" in email.subject
inbox_controller = MailSlurpClient::InboxControllerApi.new
# create two inboxes
inbox1 = inbox_controller.create_inbox_with_options({ inboxType: 'SMTP_INBOX' })
inbox2 = inbox_controller.create_inbox
expect(inbox1.email_address).to include('@mailslurp.mx')
# get smtp access for inbox
smtp_access = inbox_controller.get_imap_smtp_access({ inbox_id: inbox1.id })
# compose email
message = <<~MESSAGE_END
From: #{inbox1.email_address}
To: #{inbox2.email_address}
Subject: Test smtp email
This is a test
MESSAGE_END
Net::SMTP.start(smtp_access.smtp_server_host, smtp_access.smtp_server_port, 'greeting.your.domain',
smtp_access.smtp_username, smtp_access.smtp_password, :plain) do |smtp|
# send email
smtp.send_message message, inbox1.email_address, inbox2.email_address
end