0 votes
by (120 points)

My current C# code is (where searchCondition is a string):
List msgs = (await Imap.SearchAsync(ImapListFields.Envelope | ImapListFields.MessageStructure,
new ImapSearchParameter[] { ImapSearchParameter.Subject(searchCondition) }))
.OrderByDescending(m => m.Date).ToList();

I would like to modify this code to search for any of multiple Subjects, not just the one. I can see in the API that it is possible to write an OR SearchParameter but have neither found an example nor figured out the required syntax.

An example would be appreciated.

Applies to: Rebex Secure Mail

1 Answer

0 votes
by (70.2k points)

It is very simple, just use the ImapSearchParameter.Or method like this:

imap.Search(ImapListFields.Envelope | ImapListFields.MessageStructure,
            ImapSearchParameter.Or(
                ImapSearchParameter.Subject("condition1"),
                ImapSearchParameter.Subject("condition2"),
                ImapSearchParameter.Subject("condition3")
            ));

And similarly you can use the ImapSearchParameter.And method to AND conditions. For example like this:

imap.Search(ImapListFields.Envelope | ImapListFields.MessageStructure,
            ImapSearchParameter.Or(
                ImapSearchParameter.Subject("con1"),
                ImapSearchParameter.Subject("con2"),
                ImapSearchParameter.And(
                    ImapSearchParameter.Subject("con3"),
                    ImapSearchParameter.Subject("con4")
                )
            ));

The above example will result to con1 OR con2 OR (con3 AND con4).

by (120 points)
Thank you for your suggestions, which helped put me on the right path. Unfortunately they required further editing. In the end I ended up with this for the Or (where SearchConditions is a ImapSearchParameter array of any length):

List<ImapMessageInfo> msgs =
            (
                await Imap.SearchAsync
                (
                    ImapListFields.Envelope | ImapListFields.MessageStructure,
                    new ImapSearchParameter[] { ImapSearchParameter.Or(searchConditions) }
                )
            );
What was lacking in your responses is the inclusion of "new ImapSearchParameter[] {}".
by (70.2k points)
Please note that the search parameters are defined with params keyword:

  Search(..., params ImapSearchParameter[] parameters)

This construct is part of C# language, which allows you to omit array initialization. The required array is created by the compiler automatically.

For more information see:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/params
by (120 points)
I certainly do not want to get into a debate on array initialization. I have used the shortened form new () {} many times when the compiler readily understood the type being initialized. However my up-to-date VS 2022 Enterprise insists trying to omit the explicit initialization I showed is not acceptable, as it immediately underscores your version in red and supplies as the error: "Argument 2 cannot convert from 'Rebex.Net.ImapSearchParameter' to "Rebex.Net.ImapSearchParameter[]'. Put another way, supplying a single object with no other elaboration is not enough to initialize a required function argument array of object. I did try getting by with just the brackets encasing Or or with ' new () {}', but Visual Studio still refused to accept either. The form I showed works; I could find no other. I trust we are now on same page. I do thank you for your helpful initial suggestion and will leave it at that with no further comment on initialization. A working example in Rebex documentation could have avoided all this, had I been able to find one.
...