Fetch Messages
Fetches messages from the selected IMAP mailbox.
Code
using DRIT.Mail.Rfc.Imap;
var options = new ImapClientOptions
{
Host = "imap.example.com",
Port = 993,
SslMode = ImapSslMode.Ssl,
Credentials = new System.Net.NetworkCredential("user", "password"),
};
using var client = new ImapClient(options);
client.Connect();
client.Authenticate();
client.SelectMailbox("INBOX");
// Fetch message envelopes (headers) for the first 10 messages
var fetchOptions = new ImapFetchOptions
{
Envelope = true,
Flags = true,
};
var messages = client.Fetch(1, 10, fetchOptions);
foreach (ImapMessageInfo info in messages)
{
Console.WriteLine($"#{info.SequenceNumber}: {info.Envelope.Subject}");
Console.WriteLine($" Flags: {info.Flags}");
}
// Fetch the full body of a specific message
byte[] raw = client.FetchBody(1);
// Parse with MimeMessage.Load(new MemoryStream(raw)) if needed
client.Disconnect();
Remarks
Use ImapFetchOptions to control which parts of a message are downloaded.
Fetching only envelopes is much faster than fetching full bodies.