MailKitSimplified.Sender 2.10.0

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

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

MailKitSimplified Code Size

Sending and receiving emails sounds simple, after all, electronic mail existed decades before the Internet. If you're looking for 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 working with attachments or writing a reply. The aim of this package is to make sending and receiving emails as simple as possible!

SMTP with MailKitSimplified.Sender NuGet Downloads

Sending an email with MailKitSimplified.Sender is as easy as:

using var smtpSender = SmtpSender.Create("localhost");
await smtpSender.WriteEmail.To("test@localhost").SendAsync();

IMAP with MailKitSimplified.Receiver NuGet Downloads

Receiving emails with MailKitSimplified.Receiver is as easy as:

using var imapReceiver = ImapReceiver.Create("localhost");
var mimeMessages = await imapReceiver.ReadMail.Top(1).GetMimeMessagesAsync();

You can even monitor an email folder for new messages asynchronously, never before has it been this easy!

await imapReceiver.MonitorFolder.OnMessageArrival(m => Console.WriteLine(m.UniqueId)).IdleAsync();

Once you've got either a mime message or a message summary, replying is now equally as intuitive.

var mimeReply = mimeMessage.GetReplyMessage("<p>Reply here.</p>").From("noreply@example.com");

You're welcome. 🥲

Example Usage Development Release

The examples above will actually work with no other setup if you use something like smtp4dev (e.g. docker run -d -p 3000:80 -p 25:25 -p 143:143 rnwood/smtp4dev), but below are some more realistic examples. The cancellation token is recommended but not required.

Sending Mail

using var smtpSender = SmtpSender.Create("smtp.example.com:587")
    .SetCredential("user@example.com", "App1icati0nP455w0rd")
    .SetProtocolLog("Logs/SmtpClient.txt");
await smtpSender.WriteEmail
    .From("my.name@example.com")
    .To("YourName@example.com")
    .Bcc("admin@example.com")
    .Subject("Hello World")
    .BodyHtml("<p>Hi</p>")
    .Attach("appsettings.json")
    .TryAttach(@"Logs\ImapClient.txt")
    .SendAsync(cancellationToken);

See the MailKitSimplified.Sender wiki for more information.

Receiving Mail

using var imapReceiver = ImapReceiver.Create("imap.example.com:993")
    .SetCredential("user@example.com", "App1icati0nP455w0rd")
    .SetProtocolLog("Logs/ImapClient.txt")
    .SetFolder("INBOX");
var mimeMessages = await imapReceiver.ReadMail.Top(10)
    .GetMimeMessagesAsync(cancellationToken);

To only download the message parts you want to use:

var reader = imapReceiver.ReadMail
    .Skip(0).Take(250, continuous: true)
    .Items(MessageSummaryItems.Envelope);
var messageSummaries = await reader.GetMessageSummariesAsync(cancellationToken);

Note: MailKit returns results in ascending order by default, use messageSummaries.Reverse() to get descending results or imapReceiver.ReadMail.Top(#) to get the newest results.

To query unread emails from the IMAP server:

var messageSummaries = await imapReceiver.ReadFrom("INBOX/Subfolder")
    .Query(SearchQuery.NotSeen)
    .ItemsForMimeMessages()
    .GetMessageSummariesAsync(cancellationToken);

To asynchronously monitor the mail folder for incoming messages:

await imapReceiver.MonitorFolder
    .SetMessageSummaryItems()
    .SetIgnoreExistingMailOnConnect()
    .OnMessageArrival(OnArrivalAsync)
    .IdleAsync(cancellationToken);

To asynchronously forward or reply to message summaries as they arrive:

async Task OnArrivalAsync(IMessageSummary messageSummary)
{
    var mimeForward = await messageSummary.GetForwardMessageAsync(
        "<p>FYI.</p>", includeMessageId: true);
    mimeForward.From.Add("from@example.com");
    mimeForward.To.Add("to@example.com");
    //logger.LogInformation($"Reply: \r\n{mimeForward.HtmlBody}");
    //await smtpSender.SendAsync(mimeForward, cancellationToken);
}

See the MailKitSimplified.Receiver wiki for more information.

See Also License

Examples of things like dependency injection, a hosted service, or an ASP.NET API can also be found in the GitHub samples.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 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. 
.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.11.4-tags-v2-11-4-rc0001 74 6/22/2024
2.11.3-tags-v2-11-3-rc0001 78 6/22/2024
2.11.1-tags-v2-11-1-rc0001 75 6/20/2024
2.11.0-tags-v2-10-2-rc0001 78 6/19/2024
2.10.0 695 5/1/2024
2.9.0 2,125 12/15/2023
2.8.0 1,672 10/24/2023
2.7.0 664 9/28/2023
2.6.1 388 9/20/2023
2.6.0 318 9/20/2023
2.5.8 286 9/20/2023
2.5.7 316 9/19/2023
2.5.6 294 9/18/2023
2.5.5 408 9/14/2023
2.5.4 541 8/21/2023
2.5.3 352 8/19/2023
2.5.2 480 8/11/2023
2.5.1 476 7/30/2023
2.5.0 441 7/29/2023
2.4.7 1,213 1/25/2023
2.4.6 589 1/15/2023
2.4.5 561 1/15/2023
2.4.3 546 1/14/2023
2.4.2 583 1/10/2023
2.4.1 627 1/10/2023
2.4.0 634 12/12/2022
2.3.0 557 12/9/2022
2.2.0 579 12/6/2022
2.1.0 639 12/2/2022
2.0.0 601 12/1/2022
1.1.4 637 11/25/2022
1.1.3 587 11/24/2022
1.1.2 617 11/21/2022
1.1.1 623 11/17/2022
1.0.1 629 11/16/2022
0.2.1 670 11/12/2022
0.2.0 642 11/12/2022
0.1.3 646 11/11/2022
0.1.2.3 738 10/10/2022

2.9.0 .NET 8.0 Target Framework added
     2.5.0 Reuse existing ISmtpClient
     2.3.0 Forwarding and replies
     0.1.3 First version of SmtpSender