MailKitSimplified.Core 0.1.3

Suggested Alternatives

MailKitSimplified.Generic

dotnet add package MailKitSimplified.Core --version 0.1.3
NuGet\Install-Package MailKitSimplified.Core -Version 0.1.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.Core" Version="0.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MailKitSimplified.Core --version 0.1.3
#r "nuget: MailKitSimplified.Core, 0.1.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.Core as a Cake Addin
#addin nuget:?package=MailKitSimplified.Core&version=0.1.3

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

MailKitSimplified Development Release

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 due to how it implements the RFC standard. Unfortunately the downside of doing it all is that MailKit can be difficult to set up and use, especially the first time you go to try something like checking attachments or writing a reply. The aim of this package is to make sending (and receiving) emails as simple as possible!

Sending an email is now as easy as:

await writeEmail.To("test@localhost").SendAsync();

MailKitSimplified.Sender Usage

Setup

If you're not familiar with dependency injection then you can specify the SMTP host address like this:

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

An email sender must have a SMTP host address, and sometimes a port number, but leaving the port as the default value of 0 will normally choose the right port automatically (e.g. 25). Most companies use LDAP or something similar for behind-the-scenes authentication, but if not you can specify a network credential too.

Sending Mail

var email = smtpSender.WriteEmail
    .From("me@example.com")
    .To("you@example.com")
    .Subject("Hi")
    .Body("~");

await email.SendAsync();

Any configuration issues will throw an exception, but you can also opt to just log any exceptions and continue with a false output:

bool isSent = await smtpSender.WriteEmail
    .From("me@example.com", "My Name")
    .To("you@example.com", "Your Name")
    .Cc("friend1@example.com")
    .Bcc("friend2@example.com")
    .Subject("Hey You")
    .Body($"Hello at {DateTime.Now}.")
    .Attach("C:/Temp/attachment1.txt", "C:/Temp/attachment2.pdf")
    .Attach("./attachment3.docx")
    .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.

using MailKitSimplified.Sender.Extensions;

IHost host = Host.CreateDefaultBuilder(args)
    .ConfigureServices((context, services) =>
    {
        services.AddHostedService<ExampleNamespace.Worker>();
        services.AddMailKitSimplifiedEmailSender(context.Configuration);
    })
    .Build();

await host.RunAsync();

You'll also need the following in appsettings.json:

{
  "EmailSender:SmtpHost": "smtp.example.com"
}

Other optional settings include SmtpPort, ProtocolLog, SmtpCredential:UserName and SmtpCredential:Password.

Now you can use the fully configured IEmailSender or IEmailWriter anywhere you want with no other setup! For example:

public class EmailService
{
    private readonly IEmailWriter _writeEmail;

    public EmailService(IEmailWriter emailWriter) {
        _writeEmail = emailWriter;
    }
}

That's how sending an email can become as simple as one line of code.

await _writeEmail.To("test@localhost").SendAsync();

MailKitSimplified.Receiver Usage

Setup

If you're not familiar with dependency injection then just use this:

using var imapReceiver = ImapReceiver.Create("imap.example.com", 0, "U5ern@me", "P@55w0rd");

An email receiver must have a IMAP host address, a network credential (unless you're using something like smtp4dev), and sometimes a port number, but leaving the port as the default value of 0 will normally choose the right port automatically.

Receiving Mail

var mailboxReceiver = imapReceiver
    .ReadFrom("INBOX")
    .Skip(0)
    .Take(10);

var mimeMessages = await mailboxReceiver.GetMimeMessagesAsync();
var mimeMessageQueue = await imapReceiver
    .ReadFrom("INBOX")
    .GetMimeMessagesAsync();

Further examples (detailed MailKit IMAP server logs etc.) can be found in the 'samples' and 'tests' folders.

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

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
0.1.3 925 11/11/2022
0.1.3-beta 404 11/11/2022
0.1.2.3 960 10/10/2022
0.1.2.2 2,684 10/10/2022
0.0.0.1 933 10/10/2022