mailslurp-examples - csharp-dotnet-core3

https://github.com/mailslurp/examples

Table of Contents

csharp-dotnet-core3/csharp-dotnet-core3.sln


Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
MinimumVisualStudioVersion = 15.0.26124.0
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExampleService.Tests", "ExampleService.Tests\ExampleService.Tests.csproj", "{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Debug|x64 = Debug|x64
		Debug|x86 = Debug|x86
		Release|Any CPU = Release|Any CPU
		Release|x64 = Release|x64
		Release|x86 = Release|x86
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|x64.ActiveCfg = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|x64.Build.0 = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|x86.ActiveCfg = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Debug|x86.Build.0 = Debug|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|Any CPU.Build.0 = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|x64.ActiveCfg = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|x64.Build.0 = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|x86.ActiveCfg = Release|Any CPU
		{1789DD6D-F19E-48B9-B3AA-11F6C64EB687}.Release|x86.Build.0 = Release|Any CPU
	EndGlobalSection
EndGlobal

csharp-dotnet-core3/README.md

# CSharp DotNet Core xUnit MailSlurp example
Example MailSlurp usage using `dotnet-core-3` and `xunit`. See [MailSlurp docs](/docs/csharp/) for more.

## Setup
- Install `dotnet-core-3` sdk and cli.
- `dotnet restore`
- `dotnet build`

## Run tests
Set `API_KEY=your-api-key` in environment variables then run `dotnet test`.

csharp-dotnet-core3/Makefile

-include ../.env

install:
	dotnet restore
	dotnet build

test:
	API_KEY=$(API_KEY) dotnet test

csharp-dotnet-core3/ExampleService.Tests/ExampleTest.cs

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using mailslurp.Api;
using mailslurp.Client;
using mailslurp.Model;
using Xunit;

// Example usage for MailSlurp email API plugin
namespace ExampleService.Tests
{
    public class ExampleTest
    {
        // get an api key free at https://app.mailslurp.com
        private static readonly string YourApiKey = Environment.GetEnvironmentVariable("API_KEY", EnvironmentVariableTarget.Process);

        [Fact]
        public void CanSetupMailSlurp_AndCreateInbox()
        {
            Assert.NotNull(YourApiKey);

            // first configure your api key
            var config = new Configuration();
            config.ApiKey.Add("x-api-key", YourApiKey);

            // create an inbox controller
            var apiInstance = new InboxControllerApi(config);
            
            // then create an inbox
            var inbox = apiInstance.CreateInbox();
            Assert.NotNull(inbox);
            Assert.Contains("@mailslurp.com", inbox.EmailAddress);
        }
        
        private static readonly long Timeout = 30000L;
        private static readonly bool UnreadOnly = true;

        [Fact]
        public void CanSendEmail_ThenReceiveIt()
        {

            // first configure your api key
            var config = new Configuration();
            config.ApiKey.Add("x-api-key", YourApiKey);

            // create two inboxes
            var apiInstance = new InboxControllerApi(config);
            var inbox1 = apiInstance.CreateInbox();
            var inbox2 = apiInstance.CreateInbox();
            
            Assert.NotEqual(inbox1.EmailAddress, inbox2.EmailAddress);

            // send email from inbox1 to inbox2
            var sendEmailOptions = new SendEmailOptions()
            {
                To = new List<string>() {inbox2.EmailAddress},
                Subject = "Hello inbox2",
                Body = "Your code is: 123"
            };
            apiInstance.SendEmail(inbox1.Id, sendEmailOptions);
            
            // wait for email in inbox2 and read it
            var waitForInstance = new WaitForControllerApi(config);
            var email = waitForInstance.WaitForLatestEmail(inbox2.Id, Timeout, UnreadOnly);
            
            Assert.NotNull(email);
            Assert.Equal( inbox1.EmailAddress, email.From);
            Assert.Equal("Hello inbox2", email.Subject);
            Assert.Contains("Your code is: ", email.Body);
            
            // extract a code from email body
            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);
        }
    }
}

csharp-dotnet-core3/ExampleService.Tests/ExampleService.Tests.csproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <IsPackable>false</IsPackable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="mailslurp" Version="15.12.11" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
    <PackageReference Include="coverlet.collector" Version="1.2.0" />
  </ItemGroup>

</Project>