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.
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.
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.
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
john.doe@abc.com
to jane.doe@example.com
. Notice
the value for SmtpServer is set to 127.0.0.1
, indicating localhost.-UseSsl
to the commandSend-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
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }
john.doe@abc.com
. DevNullSmtp accepted
this message because we did not specify any domains.xyz.com
for Acceptable Domain as show below.
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
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:
passwords.txt
john.doe=secret007 jane.doe=topSecret123
%USERPROFILE%\
in the address bar.$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.
$cred
, which holds the password, is used in the command below.
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
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.