Testing Email Delivery Using a Fake SMTP Server

Sending emails is a critical part of many applications, whether you're developing a website, a mobile app, or any software that requires email functionality. But how do you test this functionality without bombarding real inboxes with test emails? The answer lies in using a fake SMTP server. In this blog, we'll explore what a fake SMTP server is, why it's useful, and how to test sending emails to one.

What is a Fake SMTP Server?

A fake SMTP server, also known as a mock or dummy SMTP server, is a tool that mimics the functionality of a real SMTP server but doesn't actually send emails to real recipients. Instead, it captures the outgoing emails for testing purposes, allowing developers to examine the content and delivery process without affecting real users. One such fake server is DevNullSmtp developed by Synametrics Technologies, Inc.

Why Use a Fake SMTP Server?

  1. Isolation: When testing email functionality, you want to keep your test emails separate from actual communication. Using a fake server ensures that test emails won't end up in real inboxes, avoiding any confusion or inconvenience.
  2. No Spam Risk: Sending a large number of test emails to real recipients could flag your emails as spam, impacting the deliverability of future emails from your domain and IP address. A fake SMTP server eliminates this risk.
  3. Cost-Effective: Sending emails through a real SMTP server can incur costs, especially if you have to send a significant volume. Using a fake server is usually free and doesn't involve any additional resources.
  4. Full Control: With a fake SMTP server, you have complete control over the test environment. You can monitor, inspect, and modify emails as needed, making it easier to diagnose issues and fine-tune your email functionality.

Setting Up a Fake SMTP Server

  • Download DevNullSMTP.
  • On Windows, download the EXE file into any folder and double-click to run. You will need JRE for Java 8 if you decide to run on any other operating system, such as Linux or Mac.
  • Optionally, specify one or more domain names or recipients full email address. When no recipients or domain names are specified, the server will work as an open-relay.
  • Configure the SMTP server in your application so emails to this fake server. You will see live SMTP communication on the screen as emails come in.
  • Optionally, you can save these messages to a folder on your local machine.

Performing a Test Run

You can use any Mail Transfer Agent (MUA), such as Mozilla Thunderbird, Microsoft Outlook, Synametrics EmailSender, or a custom application to send emails to this fake server. For demonstration purposes, let's use PowerShell on any Windows machine.

Test 1: Simple
  • Double-click the downloaded DevNullSmtp.exe to run it. Then, enter 25 for port and click Start Server.
  • You may get a message from Windows Defender Firewall asking you to allow the port. Click Allow if you want to send test messages from machines other than localhost. Click Cancel if you're sending the test emails from the same machine.
  • Next, open a PowerShell Windows and type the following command:
    Send-MailMessage -To john.doe@abc.com -from jane.doe@example.com -Subject 'Hello World' -Body 'This is a test' -SmtpServer 127.0.0.1
  • An email will be sent to DevNullSmtp and you will see the SMTP communication in the log tab, similar to the following screen.
  • This test email is sent from john.doe@abc.com to jane.doe@example.com. Notice the value for SmtpServer is set to 127.0.0.1, indicating localhost.
Test 2: Using SSL Encryption
  • Let's make this test a bit more interesting by enabling SSL encryption, which is done by adding a -UseSsl to the command
  • Send-MailMessage -UseSsl -To john.doe@abc.com -from jane.doe@example.com -Subject 'Hello World' -Body 'This is a test' -SmtpServer 127.0.0.1
  • However, this runs into an error displayed in the image below..
  • This error complains about an untrusted SSL certificate. It occurs because DevNullSmtp uses a self-signed certificate and is not trusted.
  • Type the following command in PowerShell to make Send-MailMessage CmdLet to ignore untrusted certificate.
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }
  • Sending the email the second time will work and you will see another message in DevNullSmtp.
Test 3: Relaying Denied
  • The previous two tests sent an email to john.doe@abc.com. DevNullSmtp accepted this message because we did not specify any domains.
  • Stop the server and add xyz.com for Acceptable Domain as show below.
  • Running the same test again will return the following error:
  • This error occurs because abc.com is not among acceptable domains. Changing the command in PowerShell to the following will fix the problem.
    Send-MailMessage -UseSsl -To john.doe@xyz.com -from jane.doe@example.com -Subject 'Hello World' -Body 'This is a test' -SmtpServer 127.0.0.1
Test 4: SMTP Authentication

Building on previous examples, let's make this test more engaging by adding a user ID/password for authentication. The following steps demonstrate how to add dummy users along with their passwords:

  • Create a new text file called passwords.txt
  • with the following contents:
    john.doe=secret007
    jane.doe=topSecret123
  • Save this file in the user's home folder. The easiest way to access the home folder is to open Windows File Explorer and type %USERPROFILE%\ in the address bar.
  • This will allow two users, john.doe and jane.doe, to authenticate with DevNullSMTP to send their emails.

Specifying credentials through PowerShell

  • Type the following command:
    $cred = Get-Credential
  • The following screen will prompt the user to enter their credentials and save the values into a variable called cred.

    Enter secret007 for password, which matches with the value specified in the %USERPROFILE%\passwords.txt file.

    Notice the variable $cred, which holds the password, is used in the command below.

  • Now, modify the command to use these credentials.
    Send-MailMessage -UseSsl -To john.doe@abc.com -from jane.doe@example.com -Subject 'Hello World' -Body 'This is a test' -SmtpServer 127.0.0.1 -Credential $cred

Watch A Video

Conclusion

Testing email functionality is crucial in ensuring a smooth user experience, but it's equally important to do so without affecting real users or incurring unnecessary costs. A fake SMTP server is a valuable tool for achieving this. By setting up a mock server, you can rigorously test your email sending code, inspect email content, and verify the robustness of your application's email handling processes. It's a cost-effective, efficient, and practical approach for developers aiming to deliver reliable email functionality in their applications.

Related Pages