andyedinborough / aenetmail

C# POP/IMAP Mail Client
370 stars 153 forks source link

Exchange 2003 responds with BAD Protocol Error: "Invalid key supplied in the SEARCH command" with SearchCriteria SentBefore #109

Open swhitlock opened 11 years ago

swhitlock commented 11 years ago

Here's the search criteria I'm using:

var deleteEmailCutoff = DateTime.Today
    .AddMonths(-2);

var msgs = imap.SearchMessages(
    SearchCondition.SentBefore(deleteEmailCutoff));

Here is the command sent to the server:

xm004 UID SEARCH SENTBEFORE "21-Sep-2012 12:00:00 -04"

Here is the response:

xm004 BAD Protocol Error: "Invalid key supplied in the SEARCH command".

Here's a full post of the entire exchange:

xm001 LOGIN "xyz" "lkasjflkjasflkjksl"
xm001 OK LOGIN completed.
xm002 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 IDLE LOGIN-REFERRALS MAILBOX-REFERRALS NAMESPACE LITERAL+ UIDPLUS CHILDREN
xm003 SELECT "INBOX"
* 20 EXISTS
xm004 UID SEARCH SENTBEFORE "21-Sep-2012 12:00:00 -04"
xm004 BAD Protocol Error: "Invalid key supplied in the SEARCH command".
swhitlock commented 11 years ago

In the routine ImapClient.Search(string criteria, bool uid = true), I put this hack in just to test:

string command = tag + isuid + "SEARCH " + criteria;
command = command.Replace("\"21-Sep-2012 12:00:00 -04\"", "\"21-Sep-2012\""); // HACK
string response = SendCommandGetResponse(command);

When I did that, it seemed to work correctly.

swhitlock commented 11 years ago

I did find one likely bug, though fixing it doesn't fix this problem I'm having. Here's the current GetRFC2060Date function:

internal static string GetRFC2060Date(this DateTime date) {
    return date.ToString("dd-MMM-yyyy hh:mm:ss zz", _enUsCulture);
}

Note that the "hh" used for displaying the Hour only gives you "The hour, using a 12-hour clock from 01 to 12.". The correct format string should use "HH": "The hour, using a 24-hour clock from 00 to 23.".

swhitlock commented 11 years ago

According to RFC3501 section 6.4.4:

  SENTBEFORE <date>
     Messages whose [RFC-2822] Date: header (disregarding time and
     timezone) is earlier than the specified date.

So it looks to me like some IMAP servers (likely GMail's) just ignore the time and timezone but Exchange 2003's IMAP4 server chokes on it. In light of this, I suggest the new GetRFC2060Date function should be:

internal static string GetRFC2060Date(this DateTime date) {
    return date.ToString("dd-MMM-yyyy", _enUsCulture);
}

It seems to work for me.

andyedinborough commented 11 years ago

Ah yes, the hh is a typo. Also, it seems like it should be return date.ToUniversalTime().ToString("dd-MMM-yyyy HH:mm:ss", _enUsCulture); Would that work? Could you send me a pull request?

swhitlock commented 11 years ago

I tried without the time zone (-04) and that didn't work either. The only thing I can get to work is just the date.

I don't actually have git installed, just downloaded the zip and ran it/played with it. I initially had the NuGet package.

andyedinborough commented 11 years ago

Try it with +0000 for the timezone?

Sent from my iPhone

On Nov 21, 2012, at 1:49 PM, "swhitlock" notifications@github.com<mailto:notifications@github.com> wrote:

I tried without the time zone (-04) and that didn't work either. The only thing I can get to work is just the date.

I don't actually have git installed, just downloaded the zip and ran it/played with it.

— Reply to this email directly or view it on GitHubhttps://github.com/andyedinborough/aenetmail/issues/109#issuecomment-10611589.

swhitlock commented 11 years ago

That didn't work:

xm004 UID SEARCH SENTBEFORE "21-Sep-2012 00:00:00 +0000"
xm004 BAD Protocol Error: "Invalid key supplied in the SEARCH command".

Neither did:

xm004 UID SEARCH SENTBEFORE "21-Sep-2012 00:00:00 -04:00"
xm004 BAD Protocol Error: "Invalid key supplied in the SEARCH command".