SearchCondition.To does not seem to search the actual email address
I was writing some code to search the To field for emails. Here is my sample code I was running:
static string username = "[email protected]";
static string password = "Password1";
static string host = "my.mail.server";
static int port = 993;
static string emailRecipient = "[email protected]";
static void Main(string[] args)
{
using (ImapClient Client = new ImapClient(host, port, username, password, AuthMethod.Login, true))
{
IEnumerable<uint> uids = Client.Search(SearchCondition.To(emailRecipient));
IEnumerable<MailMessage> messages = Client.GetMessages(uids);
foreach (MailMessage emailMessage in messages)
{
System.Diagnostics.Debug.WriteLine("\n\nNew Message");
System.Diagnostics.Debug.WriteLine("To: " + emailMessage.To);
System.Diagnostics.Debug.WriteLine("From: " + emailMessage.From);
System.Diagnostics.Debug.WriteLine("Headers:");
System.Collections.Specialized.NameValueCollection emailHeadersNameValueCollection = emailMessage.Headers;
foreach (String key in emailHeadersNameValueCollection.AllKeys)
{
System.Diagnostics.Debug.WriteLine("\t- " + key + " / " + emailHeadersNameValueCollection[key]);
}
System.Diagnostics.Debug.WriteLine("Body:\n" + emailMessage.Body);
System.Diagnostics.Debug.WriteLine("");
}
}
}
The SearchCondition.To fails to find the email. The header for the To field is (output from using code above and a different search condition):
To / _All My Users [email protected]
However, using the SearchCondition.To only seems to search the real name portion of the header. For example:
static string emailRecipient = "All My";
works just fine, but:
static string emailRecipient = "AllMy";
does not.
Doing this search however works with all of the above emailRecipient examples:
IEnumerable<uint> uids = Client.Search(SearchCondition.Header("To", emailRecipient));
Is the SearchCondition.To supposed to work this way or should it work as SearchCondition.Header("To", X) does?
Running Version 3.6.0.0 which I just downloaded through NuGet
The IMAP protocol defines the search criteria as follows: http://tools.ietf.org/html/rfc3501#section-6.4.4
It sounds to me like the search string should be matched against the parsed addresses in the To header (both the name and the address portions), but it's possible that your IMAP server is only matching against the parsed name strings.
SearchCondition.Header() maps to the "HEADER" search criteria which matches against the raw header value.
Thank you for the response.
It seems that even my tests against gmail produced slightly different results
It seems that in gmail I can only search the email address portion of the To field if I use a whole word. For example, I sent an email to "Casper the Ghost [email protected]". If I use SearchCondition.To with "best" or "mail" as my search string, I got nothing back. If I search for "thebest" or "gmail" then I get results.
I did not realize that the method for searching was up to the IMAP server itself. I guess for consistency, using Header it is ;)
No problem, always glad to help :-)
Hi,
I have met the same problem. I found the SearchCondition.From(a email address) works for gmail but not outlook. For outlook, it can search with real name portion, I tried to use SearchCondition.Header("From", X), but the results seems wrong. It will get emails with From "X" and CC "X". Could you please give me some other advice?
If I use SearchCondition.Header("From", X), it seems like it will math not only in From field, but from the whole header. Looking forward to your reply and thank you!