Created
December 3, 2023 20:27
-
-
Save brianmed/5adad2ba021eb33a6ea5769e32a2031a to your computer and use it in GitHub Desktop.
Download Email via IMAP with MailKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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