# MailSlurp C# Client

Create real email addresses on demand. Send and receive emails and attachments from code and tests using CSharp (C# DotNet Core).

MailSlurp is an email API service that lets you create real email addresses in code. You can then send and receive emails and attachments in C# applications and tests.

# Get started

TIP

This section describes how to get up and running with the CSharp client. The client targets DotNet-Core 3. If you need a different target consider calling the REST API.

See the method documentation for a list of all functions

# Create API Key

First you'll need an API Key. Create a free account and copy the key from your dashboard.

# Install NuGet Package

MailSlurp's CSharp library is hosted on NuGet.

# With .NET CLI

dotnet add package mailslurp
dotnet restore

# Package Manager

Install-Package mailslurp

# Configure client

Once your MailSlurp package is installed you can configure a client with your API Key.

using mailslurp.Api;
using mailslurp.Client;
using mailslurp.Model;
var config = new Configuration();
config.ApiKey.Add("x-api-key", "your_api_key_here");

# Create controllers

You can call API controllers using the corresponding ControllerApi classes.

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

# Common uses

MailSlurp has many functions. Here are some common uses:

# Create an inbox

Inboxes have an ID and a real email address.

var apiInstance = new InboxControllerApi(config);
var inbox = apiInstance.CreateInbox();
Assert.NotNull(inbox);
Assert.Contains("@mailslurp.com", inbox.EmailAddress);

# Send emails

You can send an email by first creating an inbox. Then use the inbox ID to send an email from it.

var sendEmailOptions = new SendEmailOptions()
{
    To = new List<string>() {"test@example.com"},
    Subject = "Hello",
    Body = "<h1>MailSlurp supports HTML</h1>",
};
apiInstance.SendEmail(inbox.Id, sendEmailOptions);

# Receive emails

You can receive emails using waitFor methods on the WaitForControllerApi class.

var Timeout = 30000L; // max milliseconds to wait
var UnreadOnly = true; // only count unread emails
var waitForInstance = new WaitForControllerApi(config);
var email = waitForInstance.WaitForLatestEmail(inbox.Id, Timeout, UnreadOnly);
Assert.NotNull(email);
Assert.Equal("Hello", email.Subject);
Assert.Contains("Your code is: ", email.Body);

You can extract content from email bodies using RegExps:

// imagine that email body is `Your code is: 123` and you want to get the number
var rx = new Regex(@"Your code is: ([0-9]{3})", RegexOptions.Compiled);
var match = rx.Match(email.Body);
var code = match.Groups[1].Value;
Assert.Equal("123", code);

# Next steps

See the GitHub source code for more method documentation.