How to search mails newer than a specific timestamp using Mailkit? [duplicate]
Asked Answered
M

1

5

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?

Martinic answered 30/10, 2018 at 19:43 Comment(1)
You can't. The IMAP protocol does not support granularity finer than a day. You'd have to filter the results locally.Eby
S
8

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).

Standifer answered 30/10, 2018 at 20:9 Comment(3)
Shouldn't mailkit use WITHIN for SentAfter automatically?Silicic
I was going for a more 1:1 implementation as opposed to trying to assume what the developer wanted.Standifer
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.Martinic

© 2022 - 2024 — McMap. All rights reserved.