SmtpMoq.NET is a lightweight SMTP Server that runs within the process of your .NET Core application. All the emails are not delivered but stored in the memory. Using HTML UI or JSON API you can verify if your application correctly handles the outgoing emails during development/integration testing.
The following SMTP commands are suppported:
- EHLO
- HELO
- RCPT
- DATA
- RSET
- NOOP
- QUIT
- VRFY
SSL connections are not supported since the SMTP server runs on the same machine. Also no username/paswword authentication is required.
The easies way to install SmtpMoq.NET is via NuGet.
Enter the following command in Visual Studio's Package Manager Console:
Install-Package SmtpMoq.NET
To run the SmtpMoq.NET server inside your application add the following line to the ConfigureServices
method (Startup.cs
)
services.AddSmtpMoq();
To run the HTML UI and the JSON API add the following line to the Configure
method (Startup.cs
):
app.UseSmtpMoq();
Bellow is a code snipped from the SmtpMoq.Example.WebApi.Test.E2E
sample part of the SmtpMoq.NET package. The test checks if the user registration API properly sends an account confirmation email:
[Fact]
public async Task TestAccountConfirmationEmailSent()
{
RegisterModel registerModel = new RegisterModel()
{
Email = "test@emial.com",
Password = "testPassword",
ConfirmPassword = "testPassword",
};
string modelAsJson = JsonSerializer.Serialize<RegisterModel>(registerModel);
StringContent requestContent = new StringContent(modelAsJson, Encoding.UTF8, "application/json");
var registerResponse = await httpClient.PostAsync("/accounts/register", requestContent);
Assert.Equal(HttpStatusCode.OK, registerResponse.StatusCode);
var emailResponse = await httpClient.GetAsync("/smtpmoq/api/emails/last");
Assert.Equal(HttpStatusCode.OK, emailResponse.StatusCode);
string emailResultBody = await emailResponse.Content.ReadAsStringAsync();
EmailMessage sentEmail = JsonSerializer.Deserialize<EmailMessage>(emailResultBody);
Assert.Equal("my@address.com", sentEmail.From);
Assert.Equal(registerModel.Email, sentEmail.RecipientsCommaSeparated);
Assert.Contains("https://yourdomain.com/accounts/confirm/token123", sentEmail.Data);
}
You can visually access the emails processed by the server by navigating to the folloing URL address /smtpmoq/ui
:
http://YourAppUrl/smtpmoq/ui
To file a bug or new feature, please open a new issue.
Here is the official blogpost that announces the project and the motivation. If you prefer you can comment there.