0 votes

Hi Team,

We are trying to retrieve the emails using below expression query, where the given email should be exists in TO or CC.

List<long> _objUidsLst = _objimap.Search().Where(
    Expression.And(
        Expression.HasFlag(Flag.Unseen),
        Expression.Subject("PCP"),
        Expression.Or(
            Expression.To(paramContractEmail)
            ),
        Expression.Or(
            Expression.Cc(paramContractEmail)
            )
        )
    );

If we remove the Expression.Or(), it return all the mail which are Unseen and subject contain the "PCP" word, where as with Expression.Or() or Expression.And() it return zero results.

We have already gone through https://www.limilabs.com/blog/how-to-search-imap-in-net but not results.

Best Regards,
Hiten

by (200 points)

1 Answer

0 votes

I think your query is a bit incorrect.

You need an Or above To and Cc expressions:

List<long> _objUidsLst = _objimap.Search().Where(
    Expression.And(
        Expression.HasFlag(Flag.Unseen),
        Expression.Subject("PCP"),
        Expression.Or(
            Expression.To(paramContractEmail),
            Expression.Cc(paramContractEmail)
            )
        )
    );
by (301k points)

Thank you for your response,

We have tried this way also and the result is zero.

Best Regards,

I'm sure this code is correct. Tested it on Gmail to be sure.

It generates following IMAP search query:

UID SEARCH RETURN (ALL) UNSEEN SUBJECT SubjectToCc 
OR TO to@example.com CC cc@example.com

Maybe your IMAP server doesn't implement searching properly - it happens.

Try using simpler queries: first by subject, by to, by cc, by to OR cc and so on. try establishing which part is not working on your server.

Alternatively you may use this approach:
https://www.limilabs.com/blog/get-email-information-from-imap-fast

..and search on the client side.

I have attempted to perform the simple query as well, and the result was the same.

I want to inform you that we are retrieving emails from Office 365 using OAuth authentication, as basic authentication is disabled. We have successfully retrieved all the emails and stored the relevant information in a SQL database, while saving the attachments to a file server.

Our requirement is to segregate the emails within the inbox folder by creating subfolders and applying rules.

Further, we have tried the logic you suggested, and I'm confident that Office 365 supports searching based on criteria such as To, Cc, Subject, Unseen, and more but some how it is not working in our case.

The approach you recommended, which involves managing the filtering at the client side, will certainly work well.

Best Regards,
Hiten

I tested it with Office365, even a simple AND is not working:

A001 UID SEARCH SUBJECT SubjectToCc TO to@example.com
* SEARCH

Separately they do work.

It looks like a bug on Office365 side.

I'd go with the client side approach - it will be definitely more reliable.

In case of very big inboxes, you can fine tune it with remembering last UID you received:
https://www.limilabs.com/blog/get-new-emails-using-imap

...