Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created December 3, 2023 20:27
Show Gist options
  • Save brianmed/5adad2ba021eb33a6ea5769e32a2031a to your computer and use it in GitHub Desktop.
Save brianmed/5adad2ba021eb33a6ea5769e32a2031a to your computer and use it in GitHub Desktop.
Download Email via IMAP with MailKit
using MimeKit;
using MailKit;
using MailKit.Search;
using MailKit.Security;
using MailKit.Net.Imap;
namespace MailKitDownloadImap;
class Program
{
static void Main(string[] args)
{
using (var client = new ImapClient (new ProtocolLogger ("imap.log")))
{
client.Connect ("imap.fastmail.com", 993, SecureSocketOptions.SslOnConnect);
client.Authenticate (Environment.GetEnvironmentVariable("IMAP_USERNAME"), Environment.GetEnvironmentVariable("IMAP_PASSWORD"));
client.Inbox.Open (FolderAccess.ReadOnly);
var uids = client.Inbox.Search (SearchQuery.All);
foreach (var uid in uids) {
var message = client.Inbox.GetMessage (uid);
// write the message to a file
message.WriteTo (string.Format ("{0}.eml", uid));
}
client.Disconnect (true);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment