Mail Productivity Software Development Dotnet Testing

A Productivity Game-Changer: MailSlurper for Development Email Testing

5 Feb 2024 · Rabington Chitima

Back again on the simple things that can make software development better and efficient. Today I'll cover MailSlurper.

MailSlurper is a handy tool for testing email functionality in development environments. It allows you to capture and view emails sent by your application without actually sending them to real email addresses. To show it in action I'll test with a simple C# application.

The Discovery

Like many developers, I've been forced to set up mail servers just for debugging, and ended up creating countless Gmail accounts to test and then forget about afterwards. During product/prototype presentations I'd jump from one account to another to illustrate the email notification flow. Emails like [email protected], [email protected], [email protected], etc. bring a headache when it comes to things like confirm-email, reset-link, or report emails. If you can relate to any of this, start thanking MailSlurper.

Prerequisites

  • Visual Studio — Community edition is free, from https://visualstudio.microsoft.com/downloads/.
  • MailSlurper — download the latest version from https://mailslurper.com/ and install it (demo used version 1.14.1-windows).

Setting Up and Configuring MailSlurper

  1. Install MailSlurper as per the installation instructions on the wiki.
  2. Configure MailSlurper — open config.json for Windows and ensure the SMTP server is running on the default port (usually 2500). Check the wwwPort too, and change it if it conflicts (usually defaults to 8080):
{
 "wwwAddress": "localhost",
 "wwwPort": 8080,
 "serviceAddress": "localhost",
 "servicePort": 8085,
 "smtpAddress": "localhost",
 "smtpPort": 2500,
 "dbEngine": "SQLite",
 "dbHost": "",
 "dbPort": 0,
 "dbDatabase": "./mailslurper.db",
 "dbUserName": "",
 "dbPassword": "",
 "maxWorkers": 1000,
 "autoStartBrowser": false,
 "keyFile": "",
 "certFile": "",
 "adminKeyFile": "",
 "adminCertFile": "",
 "authenticationScheme": "",
 "authSecret": "",
 "authSalt": "",
 "authTimeoutInMinutes": 120,
 "credentials": {}
}

Creating a C# .NET Project

Create a console application:

dotnet new console -n EmailAppForMailSlurper

Install MailKit via Tools → NuGet Package Manager — an open-source email framework for .NET.

using MailKit.Net.Smtp;
using MimeKit;

Simple email sending function

static void SendEmail()
{
    try
    {
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress("Employee Name", "[email protected]"));
        message.To.Add(new MailboxAddress("Approver Name", "[email protected]"));
        message.Subject = "Approval Request Email";
        message.Body = new TextPart("plain")
        {
            Text = "This is an approval request test email sent from a C# application."
        };

        using (var client = new SmtpClient())
        {
            // 2500 is the MailKit SMTP server port
            client.Connect("localhost", 2500, false);
            client.Send(message);
            client.Disconnect(true);
        }

        Console.WriteLine("Email sent successfully!");
        Console.ReadLine();
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error sending email: " + ex);
        Console.ReadLine();
    }
}

The line that matters is client.Connect("localhost", 2500, false); — it points to MailSlurper running on port 2500 on localhost. Normally this is where you'd point to a real mail server with real email addresses. If you already have an application with emailing functionality, just point it to the MailSlurper SMTP port and you're good to go.

Testing the Setup

  1. Run MailSlurper — make sure it's running.
  2. Run the application — run EmailAppForMailSlurper or your own mailing application.
  3. Check MailSlurper — open your browser and navigate to https://localhost:8080. You should see the sent email in the MailSlurper interface.

Conclusion

Configuring MailSlurper with your projects provides an efficient way to test email functionality during development. This setup allows you to capture and inspect emails without needing a real email server. Ensure your application's email-sending logic is working as expected before deploying to production. Happy testing!

← Back to blog