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!
Sending an email with MailKitSimplified.Sender is as easy as:
using var smtpSender = SmtpSender.Create("localhost");
await smtpSender.WriteEmail.To("test@localhost").SendAsync();
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. š„²
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.
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.
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.
Examples of things like dependency injection, a hosted service, or an ASP.NET API can also be found in the GitHub samples.