mailslurp-examples - csharp-dotnet-core-8-smtpclient

https://github.com/mailslurp/examples

Table of Contents

csharp-dotnet-core-8-smtpclient/SmtpClientMailKitExample/SmtpClientMailKitExample.sln


Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SmtpClientMailKitExample", "SmtpClientMailKitExample\SmtpClientMailKitExample.csproj", "{110A8203-ECD2-4EB8-9664-5C9ED1628FC9}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{110A8203-ECD2-4EB8-9664-5C9ED1628FC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{110A8203-ECD2-4EB8-9664-5C9ED1628FC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{110A8203-ECD2-4EB8-9664-5C9ED1628FC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
		{110A8203-ECD2-4EB8-9664-5C9ED1628FC9}.Release|Any CPU.Build.0 = Release|Any CPU
	EndGlobalSection
EndGlobal

csharp-dotnet-core-8-smtpclient/SmtpClientMailKitExample/SmtpClientMailKitExample/SmtpClientTest.cs

//<gen>csharp_smtpclient_import

using System.Net;
using System.Net.Mail;
using mailslurp.Api;
using mailslurp.Client;
using mailslurp.Model;

//</gen>

namespace SmtpClientMailKitExample;

public class TestsSmtpClient
{
    [Test]
    public void Test_CanCreateInboxAndSend_SmtpClient()
    {
        var YOUR_API_KEY = Environment.GetEnvironmentVariable("API_KEY");
        Assert.That(YOUR_API_KEY, Is.Not.Null);
        //<gen>csharp_smtpclient_configure_mailslurp
        var config = new Configuration();
        config.ApiKey.Add("x-api-key", YOUR_API_KEY);
        //</gen>
        //<gen>csharp_smtpclient_create_inbox
        var inboxController = new InboxControllerApi(config);
        var inbox = inboxController.CreateInboxWithOptions(new CreateInboxDto
        {
            InboxType = CreateInboxDto.InboxTypeEnum.SMTPINBOX
        });
        var accessDetails = inboxController.GetImapSmtpAccess(inbox.Id);
        //</gen>
        Assert.That(inbox.InboxType, Is.EqualTo(InboxDto.InboxTypeEnum.SMTPINBOX));
        //<gen>csharp_smtpclient_configure_smtpclient
        var client = new SmtpClient
        {
            Port = accessDetails.SecureSmtpServerPort,
            Host = accessDetails.SecureSmtpServerHost,
            EnableSsl = true,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(userName: accessDetails.SecureSmtpUsername, password: accessDetails.SecureSmtpPassword)
        };
        //</gen>
        //<gen>csharp_smtpclient_configure_send
        var mail = new MailMessage()
        {
            From = new MailAddress(inbox.EmailAddress),
            Subject = "This is a test",
            Body = "This is a test email sent from .NET application.",
        };
        mail.To.Add(inbox.EmailAddress);
        client.Send(mail);
        //</gen>
        //<gen>csharp_smtpclient_configure_receive
        var waitForControllerApi = new WaitForControllerApi(config);
        var email = waitForControllerApi.WaitForLatestEmail(inbox.Id, 120000, true);
        //</gen>
        Assert.That(email.Subject, Does.Contain("This is a test"));
    }
}

csharp-dotnet-core-8-smtpclient/SmtpClientMailKitExample/SmtpClientMailKitExample/SmtpClientMailKitExample.csproj

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

    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>

        <IsPackable>false</IsPackable>
        <IsTestProject>true</IsTestProject>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="MailKit" Version="4.3.0"/>
        <PackageReference Include="mailslurp" Version="15.17.39"/>
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
        <PackageReference Include="NUnit" Version="3.13.3"/>
        <PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
        <PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
        <PackageReference Include="coverlet.collector" Version="6.0.0"/>
    </ItemGroup>

</Project>

csharp-dotnet-core-8-smtpclient/SmtpClientMailKitExample/SmtpClientMailKitExample/MailKitTest.cs

//<gen>csharp_mailkit_import
using MailKit.Net.Smtp;
using MailKit.Security;
using MimeKit;
using mailslurp.Api;
using mailslurp.Client;
using mailslurp.Model;
//</gen>

namespace SmtpClientMailKitExample;

public class TestsMailKit
{
    [Test]
    public async Task Test_CanCreateInboxAndSend_MailKit()
    {
        var YOUR_API_KEY = Environment.GetEnvironmentVariable("API_KEY");
        Assert.That(YOUR_API_KEY, Is.Not.Null);
        //<gen>csharp_mailkit_configure_mailslurp
        var config = new Configuration();
        config.ApiKey.Add("x-api-key", YOUR_API_KEY);
        //</gen>
        //<gen>csharp_mailkit_create_inbox
        var inboxController = new InboxControllerApi(config);
        var inbox = await inboxController.CreateInboxWithOptionsAsync(new CreateInboxDto
        {
            InboxType = CreateInboxDto.InboxTypeEnum.SMTPINBOX,
            Name = "My test inbox",
        });
        var accessDetails = await inboxController.GetImapSmtpAccessAsync(inbox.Id);
        //</gen>
        Assert.That(inbox.InboxType, Is.EqualTo(InboxDto.InboxTypeEnum.SMTPINBOX));
        //<gen>csharp_mailkit_configure_message
        var message = new MimeMessage();
        message.From.Add(new MailboxAddress(name: inbox.Name, address: inbox.EmailAddress));
        message.To.Add(new MailboxAddress(name: inbox.Name, address: inbox.EmailAddress));
        message.Subject = "Test Email";
        message.Body = new TextPart("plain")
        {
            Text = @"Hello World!"
        };
        //</gen>
        //<gen>csharp_mailkit_configure_smtpclient_send
        using (var client = new SmtpClient())
        {
            await client.ConnectAsync(host: accessDetails.SecureSmtpServerHost , port: accessDetails.SecureSmtpServerPort, SecureSocketOptions.StartTls);
            // Remove other authentication mechanisms except for PLAIN
            client.AuthenticationMechanisms.Remove("XOAUTH2");
            client.AuthenticationMechanisms.Remove("CRAM-MD5");
            client.AuthenticationMechanisms.Remove("LOGIN");
            await client.AuthenticateAsync(userName: accessDetails.SecureSmtpUsername, password: accessDetails.SecureSmtpPassword);
            await client.SendAsync(message);
            await client.DisconnectAsync(true);
        }
        //</gen>
        //<gen>csharp_mailkit_configure_receive
        var waitForControllerApi = new WaitForControllerApi(config);
        var email = await waitForControllerApi.WaitForLatestEmailAsync(inbox.Id, 120000, true);
        //</gen>
        Assert.That(email.Body, Does.Contain("Hello World!"));
        //<gen>csharp_mailkit_mailslurp_send
        var sent = await inboxController.SendEmailAndConfirmAsync(inbox.Id, new SendEmailOptions(
            to: [inbox.EmailAddress],
            subject: "Hello",
            body: "Testing",
            validateEmailAddresses: SendEmailOptions.ValidateEmailAddressesEnum.VALIDATEERRORIFINVALID
        ));
        Assert.That(sent.To, Does.Contain(inbox.EmailAddress));
        //</gen>
        //<gen>csharp_mailkit_mailslurp_verify
        var verificationControllerApi = new EmailVerificationControllerApi(config);
        var results =await verificationControllerApi.ValidateEmailAddressListAsync(new ValidateEmailAddressListOptions(["contact@mailslurp.dev"]) );
        //</gen>
        Assert.That(results.ValidEmailAddresses, Does.Contain("contact@mailslurp.dev"));
        
    }
}

csharp-dotnet-core-8-smtpclient/SmtpClientMailKitExample/SmtpClientMailKitExample/GlobalUsings.cs

global using NUnit.Framework;