MailKitSimplified.Email
2.4.8-tags-v2-4-8-alp-0001
dotnet add package MailKitSimplified.Email --version 2.4.8-tags-v2-4-8-alp-0001
NuGet\Install-Package MailKitSimplified.Email -Version 2.4.8-tags-v2-4-8-alp-0001
<PackageReference Include="MailKitSimplified.Email" Version="2.4.8-tags-v2-4-8-alp-0001" />
paket add MailKitSimplified.Email --version 2.4.8-tags-v2-4-8-alp-0001
#r "nuget: MailKitSimplified.Email, 2.4.8-tags-v2-4-8-alp-0001"
// Install MailKitSimplified.Email as a Cake Addin #addin nuget:?package=MailKitSimplified.Email&version=2.4.8-tags-v2-4-8-alp-0001&prerelease // Install MailKitSimplified.Email as a Cake Tool #tool nuget:?package=MailKitSimplified.Email&version=2.4.8-tags-v2-4-8-alp-0001&prerelease
MailKitSimplified
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
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
Receiving emails with MailKitSimplified.Receiver is as easy as:
using var imapReceiver = ImapReceiver.Create("localhost");
var mimeMessages = await imapReceiver.ReadMail.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 = mimeMessages.GetReplyMessage("<p>Reply here.</p>");
You're welcome. 🥲
Example Usage
The examples above will actually work with no other setup if you use something like 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/Subfolder");
var mimeMessages = await imapReceiver.ReadMail
.Skip(0).Take(10, continuous: true)
.GetMimeMessagesAsync(cancellationToken);
To only download the email parts you want to use:
var messageSummaries = await imapReceiver.ReadMail
.GetMessageSummariesAsync(cancellationToken);
To query unread emails from the IMAP server and specify which message parts to download:
var messageSummaries = await imapReceiver.ReadFrom("INBOX")
.Query(SearchQuery.NotSeen)
.Items(MailFolderReader.CoreMessageItems)
.GetMessageSummariesAsync(cancellationToken);
To asynchronously monitor the mail folder for incoming messages:
await imapReceiver.MonitorFolder
.SetMessageSummaryItems().SetIgnoreExistingMailOnConnect()
.OnMessageArrival((messageSummary) => OnArrivalAsync(messageSummary))
.IdleAsync(cancellationToken);
To asynchronously forward message summaries as they arrive:
async Task OnArrivalAsync(IMessageSummary messageSummary)
{
var mimeReply = await messageSummary.GetForwardMessageAsync("<p>Reply here.</p>");
mimeReply.From.Add("from@example.com");
mimeReply.To.Add("to@example.com");
}
See the MailKitSimplified.Receiver wiki for more information.
See Also
Examples of things like dependency injection, a hosted service, or an ASP.NET API can also be found in the GitHub samples.
Product | Versions 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 is compatible. 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. |
-
.NETStandard 2.0
- CommunityToolkit.Common (>= 8.1.0)
- CommunityToolkit.Diagnostics (>= 8.1.0)
- CommunityToolkit.Mvvm (>= 8.1.0)
- MailKitSimplified.Generic (>= 2.0.0)
- MailKitSimplified.Receiver (>= 2.4.7)
- MailKitSimplified.Sender (>= 2.4.7)
-
net6.0
- CommunityToolkit.Common (>= 8.1.0)
- CommunityToolkit.Diagnostics (>= 8.1.0)
- CommunityToolkit.Mvvm (>= 8.1.0)
- MailKitSimplified.Generic (>= 2.0.0)
- MailKitSimplified.Receiver (>= 2.4.7)
- MailKitSimplified.Sender (>= 2.4.7)
-
net7.0
- CommunityToolkit.Common (>= 8.1.0)
- CommunityToolkit.Diagnostics (>= 8.1.0)
- CommunityToolkit.Mvvm (>= 8.1.0)
- MailKitSimplified.Generic (>= 2.0.0)
- MailKitSimplified.Receiver (>= 2.4.7)
- MailKitSimplified.Sender (>= 2.4.7)
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 |
---|---|---|
2.4.8-tags-v2-4-8-alp-0001 | 145 | 4/2/2023 |