+1 vote

Suppose I need to search with one or two conditions depending on user input. I can write

if (searchSubject)
{
    uids = searchUnseen ?
        m_imap.Search().Where(Expression.And(
            Expression.Subject("string"),
            Expression.HasFlag(Flag.Unseen)))
                :
            m_imap.Search().Where(Expression.Subject("string"));
}
else
{
    uids = searchUnseen ?
        m_imap.Search().Where(Expression.HasFlag(Flag.Unseen))
            :
        m_imap.Search().Where(Expression.All());
}

With two possibilities I hardcode 4 pieces, but with 6 there will be 64. Is there a better way?

Here is another (very small) issue. It seems to me there is a typo in Mail.dll help. Description of the MailAddress.Name property contains "Can by null". Probably there should be "Can be null"

by

1 Answer

0 votes
 
Best answer

Yes, it is possible in Mail.dll .NET IMAP client you can do something like this:

string subject = "subject";
string from = "from";
bool unseen = false;

List<ICriterion> andExp = new List<ICriterion>();

if(subject != null)
    andExp.Add(Expression.Subject(subject));

if (from != null)
    andExp.Add(Expression.From(from));

if (unseen == true)
    andExp.Add(Expression.HasFlag(Flag.Unseen));

ICriterion exp = Expression.And(andExp.ToArray());

imap.Search(exp);

Documentation spelling bug was fixed - Thanks!

by (301k points)
edited by
...