在 MailKit 中搜索电子邮件不是使用 SearchQuery 方法,而是使用简单的文本字符串条件
Search for emails in MailKit not with the SearchQuery method but with simple text string criterias
Mailkit.SearchQuery 提供了一个非常强大的搜索系统,用于查找具有各种参数的电子邮件。但我想做一项研究,从一个简单的文本字符串中获取条件,让用户能够自己进行复杂的搜索。
所以我不想这样做:
var query = SearchQuery.DeliveredAfter(DateTime.Parse("2021-10-06"))
.And(SearchQuery.FromContains("mailer-daemon@myprovider.it"));
但我想这样做:
string query = "SENTSINCE 2021-10-06 FROM mailer-daemon@myprovider.it";
问题是收件箱是 ImailFolder 对象(它没有带有简单文本字符串参数重载的 Search 方法)而不是 ImapFolder 对象(它得到了它!)。
我该怎么办?
static void ReadMsgs(Options opts)
{
using (var client = new ImapClient(new ProtocolLogger("imap.log")))
{
client.Connect(opts.Host, opts.Port, opts.UseSSL);
client.Authenticate(opts.UserName, opts.Password);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Recent);
// let's try searching for some messages...
Console.WriteLine("Search in progress...");
// this is a functional query based on SearchQuery
//var query = SearchQuery.DeliveredAfter(DateTime.Parse("2021-10-06"))
// .And(SearchQuery.FromContains("mailer-daemon@myprovider.it"));
// this is the code I would like to integrate based on a IMAP UID SEARCH text string
//string query = "SENTSINCE 2021-10-06 FROM mailer-daemon@myprovider.it";
foreach (var uid in inbox.Search(query))
{
var message = inbox.GetMessage(uid);
Console.WriteLine("{0}|{1}|{2}|{3}|{4}", uid, message.Date, message.From, message.To,message.Subject);
}
client.Disconnect(true);
}
}
您可以从 IMailFolder
投射到 ImapFolder
。
var inbox = (ImapFolder) client.Inbox;
ImapClient 返回的所有 EMail 文件夹都是 Imap 文件夹。
Mailkit.SearchQuery 提供了一个非常强大的搜索系统,用于查找具有各种参数的电子邮件。但我想做一项研究,从一个简单的文本字符串中获取条件,让用户能够自己进行复杂的搜索。
所以我不想这样做:
var query = SearchQuery.DeliveredAfter(DateTime.Parse("2021-10-06"))
.And(SearchQuery.FromContains("mailer-daemon@myprovider.it"));
但我想这样做:
string query = "SENTSINCE 2021-10-06 FROM mailer-daemon@myprovider.it";
问题是收件箱是 ImailFolder 对象(它没有带有简单文本字符串参数重载的 Search 方法)而不是 ImapFolder 对象(它得到了它!)。 我该怎么办?
static void ReadMsgs(Options opts)
{
using (var client = new ImapClient(new ProtocolLogger("imap.log")))
{
client.Connect(opts.Host, opts.Port, opts.UseSSL);
client.Authenticate(opts.UserName, opts.Password);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly);
Console.WriteLine("Total messages: {0}", inbox.Count);
Console.WriteLine("Recent messages: {0}", inbox.Recent);
// let's try searching for some messages...
Console.WriteLine("Search in progress...");
// this is a functional query based on SearchQuery
//var query = SearchQuery.DeliveredAfter(DateTime.Parse("2021-10-06"))
// .And(SearchQuery.FromContains("mailer-daemon@myprovider.it"));
// this is the code I would like to integrate based on a IMAP UID SEARCH text string
//string query = "SENTSINCE 2021-10-06 FROM mailer-daemon@myprovider.it";
foreach (var uid in inbox.Search(query))
{
var message = inbox.GetMessage(uid);
Console.WriteLine("{0}|{1}|{2}|{3}|{4}", uid, message.Date, message.From, message.To,message.Subject);
}
client.Disconnect(true);
}
}
您可以从 IMailFolder
投射到 ImapFolder
。
var inbox = (ImapFolder) client.Inbox;
ImapClient 返回的所有 EMail 文件夹都是 Imap 文件夹。