MailKitSimplified.Sender 0.1.2.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package MailKitSimplified.Sender --version 0.1.2.3
NuGet\Install-Package MailKitSimplified.Sender -Version 0.1.2.3
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="MailKitSimplified.Sender" Version="0.1.2.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MailKitSimplified.Sender --version 0.1.2.3
#r "nuget: MailKitSimplified.Sender, 0.1.2.3"
#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.
// Install MailKitSimplified.Sender as a Cake Addin
#addin nuget:?package=MailKitSimplified.Sender&version=0.1.2.3

// Install MailKitSimplified.Sender as a Cake Tool
#tool nuget:?package=MailKitSimplified.Sender&version=0.1.2.3

MailKitSimplified.Sender Build and Test results summary

Sending and receiving emails sounds simple, after all, electronic mail existed decades before the Internet. If you're looking for a an all-in-one .NET solution for email, you'll quickly discover MailKit is recommended by even the likes of Microsoft. Unfortunately for new users though, MailKit can do too much, so when I first started using it I was surprised at how many configuration steps were involved in getting it set up, and on the receiving end how poorly some real-world SMTP servers out there implement the standard. The aim of this package is to make sending an email as simple as possible.

Usage

Setup

If you're not sure what dependency injection is then just use this:

using var smtpSender = MimeMessageSender.Create("smtp.example.com");

Sending Mail

var email = smtpSender.WriteEmail
    .From("me@example.com", "My Name")
    .To("you@example.com", "Your Name")
    .To("friend1@example.com")
    .To("friend2@example.com")
    .Subject("Hey You")
    .Body("Hello World")
    .Attach("C:/Temp/attachment1.txt", "C:/Temp/attachment2.pdf")
    .Attach("./attachment3.docx");

await email.SendAsync();

An email must have a From and at least one To address, order does not matter. Setting a subject or body will overwrite previous ones to make things simpler. Any issues will throw an exception, but you can also opt to just log them and continue with a false output:

bool isSent = await smtpSender.WriteEmail
    .From("me@example.com")
    .To("you@example.com")
    .TrySendAsync();

_logger.LogInformation("Email {result}.", isSent ? "sent" : "failed to send");

Further examples (detailed MailKit SMPT server logs etc.) can be found in MailKitSimplifiedSenderUnitTests and the example solution file.

Dependency Injection

This is recommended over manual setup as the built-in garbage collector will handle lifetime and disposal.

public class Program
{
    public static async Task Main(string[] args)
    {
        IHost host = Host.CreateDefaultBuilder(args)
            .ConfigureServices((context, services) =>
            {
                services.AddHostedService<Worker>();
                ConfigureServices(services, context.Configuration);
            })
            .Build();

        await host.RunAsync();
    }

    public static void ConfigureServices(IServiceCollection services, IConfiguration configuration)
    {
        // This adds IOptions<EmailSenderOptions> from appsettings.json
        services.Configure<EmailSenderOptions>(configuration
            .GetRequiredSection(EmailSenderOptions.SectionName));
        services.AddTransient<IFileHandler, FileHandler>();
        services.AddTransient<IMimeAttachmentHandler, MimeAttachmentHandler>();
        services.AddTransient<IEmail, Email>();
        services.AddTransient<IEmailWriter, EmailWriter>();
        services.AddTransient<IEmailSender, MimeMessageSender>();
    }
}

This can then be referenced with no other setup in your service as follows:

public class EmailService {

    private readonly IEmailSender _smtpSender;

    public EmailService(IEmailSender smtpSender) {
        _smtpSender = smtpSender;
    }
}

Receiving Mail

Coming in a sister package in the near future.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on MailKitSimplified.Sender:

Package Downloads
MailKitSimplified.Email

Easy, fluent way to send, receive, forward, and reply to emails with MailKit.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.10.0 137 5/1/2024
2.10.0-tags-v2-10-0-rc-0001 292 1/9/2024
2.9.0 1,383 12/15/2023
2.8.3-tags-v2-8-3-rc1-0001 210 12/15/2023
2.8.2-tags-v2-8-2-rc1-0001 250 11/20/2023
2.8.0 1,350 10/24/2023
2.7.0 652 9/28/2023
2.6.1 381 9/20/2023
2.6.0 317 9/20/2023
2.5.8 285 9/20/2023
2.5.7 309 9/19/2023
2.5.6 293 9/18/2023
2.5.5 399 9/14/2023
2.5.4 533 8/21/2023
2.5.3 351 8/19/2023
2.5.2 479 8/11/2023
2.5.1 475 7/30/2023
2.5.0 439 7/29/2023
2.4.7 1,203 1/25/2023
2.4.6 586 1/15/2023
2.4.5 560 1/15/2023
2.4.3 544 1/14/2023
2.4.2 581 1/10/2023
2.4.1 626 1/10/2023
2.4.0 632 12/12/2022
2.3.0 556 12/9/2022
2.2.0 578 12/6/2022
2.1.0 637 12/2/2022
2.0.0 599 12/1/2022
1.1.4 633 11/25/2022
1.1.3 586 11/24/2022
1.1.2 616 11/21/2022
1.1.1 622 11/17/2022
1.0.1 627 11/16/2022
0.2.1 666 11/12/2022
0.2.0 635 11/12/2022
0.1.3 644 11/11/2022
0.1.2.3 724 10/10/2022