ay.Aspire.For.MailKit 1.0.0

dotnet add package ay.Aspire.For.MailKit --version 1.0.0
                    
NuGet\Install-Package ay.Aspire.For.MailKit -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ay.Aspire.For.MailKit" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ay.Aspire.For.MailKit" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ay.Aspire.For.MailKit" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ay.Aspire.For.MailKit --version 1.0.0
                    
#r "nuget: ay.Aspire.For.MailKit, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ay.Aspire.For.MailKit@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ay.Aspire.For.MailKit&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ay.Aspire.For.MailKit&version=1.0.0
                    
Install as a Cake Tool

ay.Aspire.For.MailKit

This project implements Create custom .NET Aspire hosting integrations and hooks up Aspire with MailKit as described.

Integration

To integrate MailKit with Aspire use the following example. The integration will spin up a MailDev container where you will receive your emails.

The connection string is stored under 'ConnectionString:SmtpServer', either by using MailKit and environment variables or by configuring it in the appsettings.json. During the use of Aspire, the connection string will be provided in the environment variable ConnectionString__SmtpServer by Aspire.

Hosting

In your Aspire project integrate MailKit like so:


var builder = DistributedApplication.CreateBuilder(args);

var maildev = builder.AddMailDev("SmtpServer");

var webApi = builder.AddProject<[REPLACE WITH YOUR PROJECT]>("webApi")
    .WithReference(maildev);

builder.Build().Run();

Client

In your client hosted by Aspire, integrate like so:

builder.AddMailKitClient("SmtpServer");

Then inject the MailKitClientFactory like so into your controller or service. The factory will create an SmtpClient instance that already authenticated to the server if the server supports authentication and if the username is not empty. Make sure that you dispose the SmtpClient instance properly by using using or by calling .Dispose() on the instance.

app.MapPost("/subscribe",
    async (MailKitClientFactory factory, string email) =>
    {
        using var message = new MailMessage("newsletter@yourcompany.com", email)
        {
            Subject = "Welcome to our newsletter!",
            Body = "Thank you for subscribing to our newsletter!"
        };

        using var client = await factory.GetSmtpClientAsync();
        
        await client.SendAsync(MimeMessage.CreateFromMailMessage(message));
        await client.DisconnectAsync(true);
    });

And create your ISmtpClient to send an email.

To configure the connection to the SMTP server use

{
  ...
  "ConnectionStrings": {
    "SmtpServer": "Endpoint=smtp://smtp.example.com:587;Username=foo;Password=bar;DisableHealthChecks=true;DisableTracing=true;DisableMetrics=true",
  },
  ...
}

In case you only have an endpoint without authentication, you can shorten it to

{
  ...
  "ConnectionStrings": {
    "SmtpServer": "smtp://smtp.example.com:587",
  },
  ...
}

Alternatively use MailKit settings, but note that a ConnectionString will override the MailKit settings.

{
  ...
  "MailKit": {
    "Client": {
      "Endpoint": "smtp://smtp.example.com:587",
      "Username": "foo",
      "Password": "bar",
      "DisableHealthChecks": true,
      "DisableTracing": true,
      "DisableMetrics": true
    }
  }
  ...
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 156 6/24/2025
0.4.2 142 6/16/2025
0.4.1 242 6/13/2025
0.4.0 250 6/13/2025
0.3.0 302 6/12/2025
0.2.2 298 6/11/2025
0.2.0 293 6/11/2025