Email testing for developers with Mailtrap

Email testing for developers can be annoying

Do you remember the last time you worked on a project that had email integration? Do you remember when you spammed all your colleagues because you had to test the email capabilities of your application? Email testing for developers, while not a difficult task, can be very annoying if we lack some tools to help us. Here is where Mailtrap comes into play.

Good tooling can help us a lot in our testing efforts

To quote their site, Mailtrap is a fake SMTP server for development teams to test, view and share emails sent from the development and staging environments without spamming real customers.

I started to use it recently in one of my projects and these are the things that I liked:

  • It’s free for individual developers. There are some features missing, but the free capabilities saved me a lot of time and allowed me to test my application faster and without any hassle. There are also subscriptions if you want to take advantages of the full features, but you probably won’t need them.
  • It’s online. I didn’t have to install anything on my machine. I just grabbed the configuration for my mail server, filled it in my application and I was done.
  • The emails that reach Mailtrap are not automatically forwarded to the real inbox. Instead they are trapped in this mail server and you get to decide if you want to forward them to the real users or not. So, no more spamming, even if you use real email addresses: D
  • There are a couple of integration examples for most programming languages (C#, Java, Ruby, Python, PHP, etc.) to get you started fast.

Example, C# client with Mailtrap

Let’s look at an example:

This is my Mailtrap dashboard. On the left, you can see the current SMTP settings for the server. We can use them to configure our application. And we can reset them to new values if we want to.

Email testing for developers

Mailtrap server configuration

Next, I will write a simple C# console application that uses those settings to send emails. Here’s the source code bellow.

using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace ConsoleEmailSender
    class Program
        // Define properties as written in MailTrap
        private static readonly int EmailClientPort = 465;
        private static readonly string EmailClientHost = "";
        private static readonly bool EmailClientEnableSSL = true;
        private static readonly string EmailClientUserName = "02dfe73d146d08";
        private static readonly string EmailClientPassword = "9481330d718f63";

        static void Main(string[] args)
            // Create email sender with properties defined bellow
            var smtpClient = new SmtpClient(EmailClientHost, EmailClientPort)
                EnableSsl = EmailClientEnableSSL,
                Credentials = new NetworkCredential(EmailClientUserName, EmailClientPassword),
                DeliveryMethod = SmtpDeliveryMethod.Network

            // Create mail message
            var from = "";
            var to = "";
            var subject = "Hello from C# mail sender";
            var body = "This is a test email message sent from a C# app";
            var mailMessage = new MailMessage(from,to, subject, body);

            // Send email

            Console.WriteLine("Your email was sent. Please check 'Mailtrap'");

Let’s run the application and look at Mailtrap again. We have a new mail in our inbox. We just tested our application’s email capability. The mail is trapped here until we decide what to with it, so our colleagues will thank us for not spamming them. Cool 😊Email testing for developers