Disposable email accounts for DotNET core 6 are incredibly useful.

Create real throw-away email addresses for testing and development in .NET core. Follow this guide to get started.

  • Table of contents

Building applications in .NET is fun and productive but what do you do when you need to add email integrations or test using disposable email accounts? MailSlurp's free DotNET SDK client on nuget lets you create real inboxes on demand for sending and receiving emails in C#, F# and more.

Installing the latest .NET core 6

DotNET was never known for simple naming conventions. Microsoft's open source .NET 6 aims to fix some of the confusing while adding many new performance upgrades and features. To install on Linux 21.04 run:

wget https://packages.microsoft.com/config/ubuntu/21.04/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
sudo dpkg -i packages-microsoft-prod.deb
rm packages-microsoft-prod.deb

The use aptitude to install the sdk:

sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y dotnet-sdk-6.0

Then install the runtimes:

sudo apt-get update; \
  sudo apt-get install -y apt-transport-https && \
  sudo apt-get update && \
  sudo apt-get install -y aspnetcore-runtime-6.0

If you encounter errors about missing packages you may need to purge your dpkg packages

Running dotnet --version should now display version 6.

dotnet --version
> 6.0.101

Starting a new dotnet core project

To scaffold a new application that we can add disposable email accounts to use the dotnet new command in the CLI.

dotnet new console --name dotnet-6

This will create a new folder with a structure like so:

├── dotnet-6.csproj
├── obj
│   ├── dotnet-6.csproj.nuget.dgspec.json
│   ├── dotnet-6.csproj.nuget.g.props
│   ├── dotnet-6.csproj.nuget.g.targets
│   ├── project.assets.json
│   └── project.nuget.cache
└── Program.cs

Add MailSlurp dependencies to DotNET project

You can add disposable email accounts to your .NET core project easily using the dotnet CLI:

dotnet add package mailslurp

Our csproj file will be updated to include our dependency and look something like this:

<Project Sdk="Microsoft.NET.Sdk">


    <PackageReference Include="mailslurp" Version="15.1.1" />


Make sure you create a free MailSlurp account to obtain a free API_KEY. We will need that for the next steps.

Configure MailSlurp client for email creating

To configure inboxes import the MailSlurp dependency in your Program.cs file and configure it like below:

using System;
using mailslurp.Api;
using mailslurp.Client;

void Assert(bool condition, string message)
    if (!condition)
        throw new Exception(message);

var apiKey = Environment.GetEnvironmentVariable("API_KEY");
Assert(apiKey != null, "Please set missing API_KEY variable");

// configure MailSlurp with api key
var config = new Configuration();
config.ApiKey.Add("x-api-key", apiKey);

// create an inbox controller 
var apiInstance = new InboxControllerApi(config);

Using disposable email accounts in dotnet to send and receive emails

Now you are probably wondering - how do I use these email addresses? Well with MailSlurp you can create email inboxes and then send and receive emails from them using any email provider, the online dashboard, or in code.

Creating an email address

First step, create an inbox for your test:

// create a new inbox
var inbox = apiInstance.CreateInbox();
Assert(inbox.EmailAddress.Contains("@mailslurp"), "Email address is a mailslurp address");

// list the inbox emails
Assert(apiInstance.GetInboxEmailCount(inbox.Id).TotalElements == 0, "No emails in inbox");

Send an email in C Sharp

Sending emails with MailSlurp is simple:

// send an email from the inbox to itself
apiInstance.SendEmail(inbox.Id, new SendEmailOptions(to: new List<string>{ inbox.EmailAddress }, subject: "Test subject", body: "Hello"));

Receive emails

You can receive emails in .NET using MailSlurp too:

// wait for the email to arrive (block until it is received)
var latestEmail= new WaitForControllerApi(config).WaitForLatestEmail(inbox.Id);
Assert(latestEmail.Subject == "Test subject", "Has subject");
Assert(latestEmail.Body.Contains("Hello"), "Has body");

Notice the WaitForControllerApi usage. This is a unique feature of MailSlurp that enables you to block the execution and wait for the email you sent to arrive. This is important to avoid race conditions.