Email testingSMTP

Testing with SMTP

Use Mailosaur as a dummy SMTP server to catch every email your product sends.

The benefits of testing with SMTP

Whilst you can always use your server’s wildcard domain to create an unlimited number of email addresses, it is sometimes better to just send directly to Mailosaur via SMTP.

Cheaper than sending via your Email Service Provider (ESP)

If you have thousands of emails to test each day, sending these via your usual ESP (e.g. SendGrid or Amazon SES) can be unnecessarily expensive, as you’ll pay for each email you send, regardless of whether it is simply for testing purposes or not.

Sending lots of test emails via your email service provider can also be slow, as ESPs will often queue up emails and send them out in batches.

Prevent development and staging environments sending to real customers

When connecting directly to Mailosaur, every email you send is captured, regardless of who the email is sent “to”. This is great for protecting yourself from accidentally sending emails to real customers.

Where do I find my SMTP credentials?

Each server has a unique username and password usable for SMTP. To get this:

  1. Log into the Mailosaur Dashboard.
  2. Navigate to the server you want to use.
  3. Click the Settings button
  4. Select Send via SMTP.
  5. Copy the credentials from this screen.

How do I send via SMTP?

You can use these code snippets to conditionally send directly to Mailosaur in your development and/or staging environments:

const nodemailer = require('nodemailer');

(async () => {
  const transport = nodemailer.createTransport({
    service: 'Mailosaur',
    auth: {
      user: 'SERVER_ID@mailosaur.net',
      pass: 'SMTP_PASSWORD'
    }
  });

  await transport.sendMail({
    subject: 'A test email',
    from: 'Our Company <from@example.com>',
    to: 'Test User <to@example.com>',
    html: '<p>Hello world.</p>',
    text: 'Hello world.'
  });
})();
import smtplib

sender = "Our Company <from@example.com>"
recipient = "Test User <to@example.com>"

message = f"""\
Subject: A test email
To: {recipient}
From: {sender}

Hello world."""

with smtplib.SMTP("smtp.mailosaur.net", 2525) as server:
    server.login("SERVER_ID@mailosaur.net", "SMTP_PASSWORD")
    server.sendmail(sender, recipient, message)
// Uses javax.mail
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.mailosaur.net");
props.put("mail.smtp.port", "2525");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.protocols", "TLSv1.2");
props.put("mail.smtp.ssl.trust", "smtp.mailosaur.net");

Session session = Session.getInstance(props, new Authenticator() {
  @Override
  protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication("SERVER_ID@mailosaur.net", "SMTP_PASSWORD");
  }
});

MimeMessage message = new MimeMessage(session);

message.setSubject("A test email");
message.setFrom(new InternetAddress("Our Company <from@example.com>"));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress("Test User <to@example.com>"));

MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent("<p>Hello world.</p>", "text/html; charset=utf-8");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimeBodyPart);

message.setContent(multipart);

session.getTransport("smtp").send(message);
using MimeKit;
using MailKit.Security;
using MimeKit.Text;

var message = new MimeMessage();
message.From.Add(new MailboxAddress("Our Company", "from@example.com"));
message.To.Add(new MailboxAddress("Test User", "to@example.com"));
message.Subject = "A test email";

message.Body = new TextPart(TextFormat.Html) {
  Text = "<p>Hello world.</p>"
};

using (var emailClient = new MailKit.Net.Smtp.SmtpClient()) { 
  emailClient.Connect("smtp.mailosaur.net", 2525, SecureSocketOptions.StartTls) 
  emailClient.Authenticate("SERVER_ID@mailosaur.net", "SMTP_PASSWORD");
  emailClient.Send(message);
  emailClient.Disconnect(true);
}
require 'net/smtp'

message = <<MESSAGE_END
From: Our Company <from@example.com>
To: Test User <to@example.com>
Subject: A test email

Hello world.
MESSAGE_END

Net::SMTP.start(
  'smtp.mailosaur.net',
  2525,
  'smtp.mailosaur.net',
  'SERVER_ID@mailosaur.net', 'SMTP_PASSWORD', :cram_md5
) do |smtp|
  smtp.send_message message, 'from@example.com', 'to@example.com'
end
// Install with `composer require nette/mail`
require_once('vendor/autoload.php');

$smtp = new Nette\Mail\SmtpMailer([
  'host' => 'smtp.mailosaur.net',
  'port' => 2525,
  'username' => 'SERVER_ID@mailosaur.net',
  'password' => 'SMTP_PASSWORD',
  'secure' => 'starttls',
]);

$message = new Nette\Mail\Message;
$message->setFrom('Our Company <from@example.com>')
  ->addTo('Test User <to@example.com>')
  ->setSubject('A test email')
  ->setHtmlBody('<p>Hello world.</p>');

$smtp->send($message);
import (
  "fmt"
  "log"
  "net/smtp"
)

func main() {
  from := "from@example.com"
  to := []string{
    "to@example.com",
  }
  msg := []byte("From: Our Company <from@example.com>\r\n" +
    "To: Test User <to@example.com>\r\n" +
    "Subject: A test email\r\n\r\n" + 
    "Hello world.\r\n"
  )

  auth := smtp.CRAMMD5Auth("SERVER_ID@mailosaur.net", "SMTP_PASSWORD")
  err := smtp.SendMail("smtp.mailosaur.net:2525", auth, from, to, msg)
  if err != nil {
    log.Fatal(err)
  }
  fmt.Println("Mail sent successfully!")
}