Getting Started with .NET

Learn how to automate email testing in .NET

Before you start

If you don’t already have one, create a Mailosaur account.

Make sure you know how to send email to Mailosaur first. Once you have this working, you’re ready to start testing!

Installing Mailosaur

Mailosaur provides an official .NET client library to make testing email in .NET a lot easier.

Install via NuGet

From the command line:

nuget install mailosaur

From Package Manager:

PM> Install-Package mailosaur

From within Visual Studio:

  1. Open the Solution Explorer.
  2. Right-click on a project within your solution.
  3. Click on Manage NuGet Packages…
  4. Click on the Browse tab and search for “Mailosaur”.
  5. Click on the Mailosaur package, select the appropriate version in the right-tab and click Install.

Authenticating with Mailosaur

In order to connect to Mailosaur, you need to add your API key to your tests. You access your API key via the account settings screen.

Retrieving an email

Once you have sent an email to test, for example by emailing something@{SERVER_ID}, you can now use the code below to retrieve the message in .NET:

// 1. Create an instance of the MailosaurClient, using your API key
var client = new MailosaurClient("{API_KEY}");

// 2. Build search criteria to find the email you have sent
var criteria = new SearchCriteria() {
  SentTo = "something@{SERVER_ID}"

// 3. Wait for the message (by default only looks for the most recent message received in the last hour)
var message = client.Messages.Get("{SERVER_ID}", criteria);

// 4. Assert that the email subject is what we expect
Assert.Equal("Reset your password", message.Subject);

Mailosaur extracts any links found within both the HTML and text content of an email. These are then made available via message.Html.Links and message.Text.Links.

You can use these links for many scenarios, such as completing a password reset process:

// Wait for message
var message = client.Messages.Get("{SERVER_ID}", criteria);

// Check that we have the password reset email
Assert.Equal("Reset your password", message.Subject);

// Store the password reset link (e.g. to use in a Selenium test that completes the process)
var passwordResetLink = message.Text.Links[0].Href;

Extracting content from email HTML

If you need to extract content from the body of an email, such as a verification code, then you can use a library in your tests.

Let’s imagine that message.Html.Body contains:

  <h1>To activate your account, enter this code:</h1>
  <p class="verify-code">542163</p>

To work with the email’s HTML, install HTML Agility Pack. For example, with the dotnet CLI:

dotnet add package HtmlAgilityPack --version 1.11.24

Import dependencies at the top of your test file:

using HtmlAgilityPack;

Now, you can extract the verification code as follows:

// Wait for the message
var message = client.Messages.Get("{SERVER_ID}", criteria);

// Extract the verification code from HTML
var doc = new HtmlDocument();

// Find element using xpath
var node = doc.DocumentNode.SelectSingleNode("//*[contains(@class, 'verify-code')]")

var verificationCode = node.InnerText;