Search code examples
c#imap

Imap Fetch Command


I am using the following C# code retrieve the header of messages, but I want to modify it so it can tell me if the message contains any Attachments, however I do not want to load the complete message with attachments

var command = string.Format("FETCH {0} (FLAGS UID BODY[HEADER])", i);
Clean();
SendCommand(command);
var sizeLine = ReadLine();

I've tried the following

This retrieves the complete message or simply don't work, I am also working from the rfc website but can't seem to find the command for it

var command = string.Format("FETCH {0} (FLAGS UID BODY.PEEK[])", i);
var command = string.Format("FETCH {0} (FLAGS UID BODY[])", i);
var command = string.Format("FETCH {0} (FLAGS UID BODY[HEADER,MIME])", i);
var command = string.Format("FETCH {0} (FLAGS UID BODY[MIME])", i);

Solution

  • The command you're looking for is UID FETCH ... BODYSTRUCTURE. Once you've seen the response, you may wish to consider using a library to do the parsing for you. There are at least two candidates on C#, MailKit and something called, IIRC, mailsystem.net. Try mailkit first.