Easy, fluent way to receive, forward, and reply to emails with MailKit.
$ dotnet add package MailKitSimplified.ReceiverSending 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.GetMimeMessagesAsync();You can even monitor an email folder for new messages asynchronously, never before has it been this easy!
await imapReceiver.MonitorFolder.IdleAsync();The examples above will actually work with no other setup if you use something like smtp4dev, but below are some more realistic examples.
using var smtpSender = SmtpSender.Create(""smtp.gmail.com:587")
.SetCredential("user@gmail.com", "ApplicationP455w0rd")
.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();See the MailKitSimplified.Sender wiki for more information.
using var imapReceiver = ImapReceiver.Create("imap.gmail.com:993")
.SetCredential("user@gmail.com", "ApplicationP455w0rd")
.SetProtocolLog("Logs/ImapClient.txt");
var mimeMessages = await imapReceiver.ReadFrom("INBOX")
.Skip(0).Take(10).GetMimeMessagesAsync();To only download the email parts you want to use:
var messageSummaries = await imapReceiver.ReadFrom("INBOX/Subfolder")
.GetMessageSummariesAsync(MessageSummaryItems.UniqueId);To asynchronously monitor the folder for incoming messages:
var imapIdleClient = imapReceiver.Monitor("INBOX");
imapIdleClient.MessageArrivalMethod = messageSummary => Process(messageSummary);
imapIdleClient.MessageDepartureMethod = messageSummary => null;
await imapIdleClient.IdleAsync();See the MailKitSimplified.Receiver wiki for more information.