3

I am building a C# function to periodically fetch email messages from a local email server(hMailServer) and save to a database table.

var uid_list = await inbox.SearchAsync(SearchQuery.SentAfter(lastDateTime));

The problem is that it ignores the time part only compare date part. Question: how to search mails newer than a timestamp value using Mailkit?

jstedfast
  • 35,744
  • 5
  • 97
  • 110
Jiping
  • 700
  • 1
  • 9
  • 12
  • 1
    You can't. The IMAP protocol does not support granularity finer than a day. You'd have to filter the results locally. – Max Oct 30 '18 at 19:59

1 Answers1

7

If your IMAP server supports the WITHIN extension, you can use SearchQuery.YoungerThan (int seconds).

To check if your IMAP server supports this extension, you can do:

if (client.Capabilities.HasFlag (ImapCapabilities.Within)) { }

(where client is an ImapClient instance that is both connected to the IMAP server and in an authenticated state).

jstedfast
  • 35,744
  • 5
  • 97
  • 110
  • Shouldn't mailkit use WITHIN for `SentAfter` automatically? – arnt Oct 31 '18 at 09:22
  • I was going for a more 1:1 implementation as opposed to trying to assume what the developer wanted. – jstedfast Oct 31 '18 at 12:31
  • 1
    YoungerThan seems the solution. Unfortunately, my email server hMailServer does not support "Within" as YoungerThan shows. While SendAfter use granularity on Date level. My final solution is to fetch Uid list from IMAP server since last date(not datetime) and another list from the database and compare the two list to get the the difference. – Jiping Nov 01 '18 at 15:34