Writing Unit Test for SMTP Component


I was working on a notification system that allows users to subscribe an email notification based on the status of the database record. An email will be sent to the destination whenever the status is satisfied.

As the system is providing various of email templates for users to choose when they’re defining the subscription. So the challenge to the development team is writing the unit test for each template and ensure the code coverage is more than 90% according to the DevOps strategy.

Writing this post, I would like to share the experience to write the unit tests for SMTP and how to perform the system integration test (SIT) without Exchange Email Server.

I. Writing Unit Tests For SMTP

As in the email template, I allow users to add some predefined parameters into email subject and body as the sample below:

  •  [DateTime]: before delivery email to destinations the system will transform this token to current date time.
  • [RecordId]: the system will pick up the primary key value of the record and replace to this token.
  • [CreatedBy] or [UpdatedBy]: if these tokens are in email subject it will be transformed to account email from active directory. If it is in email body then will be transferred to account name instead.

The requirement of the unit test for this system is able to verify not only template transformation but also the SMTP communication in order to limit the number of the code issues bugs leaking to SIT and UAT.

I found a useful open source library named netDumbster allows to simulate an SMTP service for developers to write the unit tests for SMTP module. This module allows running the unit tests in any build server without any additional installation needed.

Below is the source code for a sample notification service

And here is the unit test for this service.

The configuration of the Unit Test project.

You may face below issue when using this library. Because the SMTP is not stopping property and the unit tests process is still running.

To resolve this issue just simply force stop below processes in the Task Manager and then run the unit tests again.

Here is the result when running the auto build on the VSTS.

I would recommend using [AssemblyInitialize] and [AssemblyCleanup] to start and stop the SMTP emulator instead of [TestInitialize] and [TestCleanup] because start and stop the emulator is taking time and sometimes the SMTP is not stopped properly and keep holding the port 25 so that the next instance is not able to initialize for the second test method.


II. The SMTP Emulator for SIT and UAT.

There are so many reasons that we don’t want The testers using the real Product exchange server to perform the SIT or UAT testing. I don’t want to list out the reason here. However, if you are looking for an alternative solution that allows testers testing the SMTP without exchange server then there is a free and open source tool for that purposes.

The Papercut is an SMTP emulator that saving the incoming emails to a location instead of delivery to the real destinations. This tool provides a WinForms application that allows tester verify the email directly.

There is a Papercut service allows you host the SMTP emulator on to a server in the test environment and share across to all application.

III. Source Code

You can download the demo source code here.

Makes Your Laptop Becomes A Build Agent Of VSTS


You are a developer and working on some open source projects. You are using VSTS to maintain your source code. As you know the free version of VSTS is giving 240 minutes every month for the build. What happens if your free minutes have gone? You need to wait for until to next month to build and deploy your projects?

You are using the preview version of Visual Studio (currently is 2017 preview 3) to develop some projects on .Net Core 2 and .Net Standard 2 preview 2. However, the is no available build agent on VSTS for the preview framework yet.

I have a crazy idea why don’t use our laptop as a build agent for VSTS so that you can install whatever you want on your laptop worry less about the host compatible? If you are facing some difficulties above then this post I will show you how to makes your laptop becomes a build agent of VSTS.

1. Download Agent Package

Login to your VSTS and navigate to the Agent Queues and then click download agent. There is three package available for Window OS X and Linux. You shall able to download a zip package around 83MB for your window. This package contains all needed components for a build agent host.


2. Generate Personal access tokens

Click on you account icon at the top right and navigate to security then Personal access tokens. Create a new token named build-agent with the Authorized Scopes as below.

After clicking the save button, you will have a token look like this:


Refer here for details of Personal access token creating and revoking.

3. Agent Installation

  • Right click on the downloaded zip package -> properties and check the unblock checkbox then click ok.
  • Unzip the package into C:\vsts-agent.
  • Create a folder C:\vsts-build for the build this one will be used to contain the source code from VSTS when executing a build definition.
  • Run the PowerShell with administrator privilege and point the unzipped package (by using cd command).
  • Run the ./config.cmd to start the installation:

  • When running agent as service it will ask for the service account, I’m using Network Service as the service account for the agent. The reason to run the agent as service is whenever you start your laptop/Pc up the agent will be available in the VSTS immediately without any configuration needed.

Run the command .\config.cmd remove to un-install the agent on your laptop if need.

I like the way Microsoft develop the agent, your laptop doesn’t need a static IP for it, The agent will work whenever the laptop is up and connected to the internet care less about the IP and location. Bring your laptop everywhere you want and execute the build anytime you need.

4. Agent Configuration

Now navigate to the default agent pool on your VSTS your laptop name should appear. Before executing the build, you need to define the capabilities of the new agent as below. The Java capabilities on the below screenshot are needed if you are using some Java build tasks example Sonarqube tasks.


Here is the full text of the capabilities of my laptop for your reference purpose.

  • java: C:\Program Files\Java\jdk1.8.0_131
  • jdk: C:\Program Files\Java\jdk1.8.0_131
  • jdk_8: C:\Program Files\Java\jdk1.8.0_131
  • MSBuild: C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise\MSBuild\15.0\Bin
  • VisualStudio: C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise
  • VSTest: C:\Program Files (x86)\Microsoft Visual Studio\Preview\Enterprise\Common7\IDE\CommonExtensions\Microsoft\TestWindow

Now you can execute your build without the limitation. However, please note that this one for personal use only. I don’t recommend to use this on your company as exposing a server to the internet is not compliance and dangerous.

If your company is using VSTS, then should purchse the agents from Micrsoft or hosting everything (TFS and Agents) on the premises to maximize the flexibility is also a case.

This is the screenshot to prove that my laptop is working fine with VSTS.