Mark Rabjohn
Mark Rabjohn

Reputation: 1713

EWS SearchFilter.ContainsSubstring to filter on Sender Email Address

I'm trying to filter emails on Exchange Web Services using SearchFilter.ContainsSubstring as follows:

sfilter = New SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, EmailAddress, ContainmentMode.Substring, ComparisonMode.IgnoreCase)
MailItems = service.FindItems(Folder.Id, sfilter, view)

Unfortunately this doesn't work, and I don't want to use Queries, because I can't guarantee that I can use features of Exchange Server 2013.

Composing a variety of requests in Fiddler, I can observe that if I remove the last character of the email address, then the filter works, remove the first character instead, works - put them back, broken.

So perhaps it's pedantic, and it has to be a true substring to qualify, so if I change the Containment mode to FullString - it doesn't work, so I can't do anything like a collection with Substring OR FullString.

It looks like I'll be able to do (Substring with last char missing AND Substring with first char missing), but it surely can't be that broken can it?

What can I do to get this to work?

Note that my code is in VB.NET, but I can't imagine that this is the problem.

Cheers,

Mark

Upvotes: 5

Views: 7249

Answers (3)

panksy2k
panksy2k

Reputation: 430

if(emailSenderList.size() == 1) { return new SearchFilter.IsEqualTo(EmailMessageSchema.From, emailSenderList.get(0)); }

return new SearchFilter.SearchFilterCollection(LogicalOperator.Or, emailSenderList.stream().map(em -> new SearchFilter.IsEqualTo(EmailMessageSchema.From, em)).toArray(SearchFilter.IsEqualTo[] :: new));

Upvotes: 0

Mark Rabjohn
Mark Rabjohn

Reputation: 1713

I worked out that the IsEqualTo filter works with From/Sender, and it doesn't care about case-sensitivity issues, so it's probably what I should have tried to begin with.

The code to match an email address is:

sfilter = New SearchFilter.IsEqualTo(EmailMessageSchema.From, New EmailAddress(Message.FromAddress))
MailItems = service.FindItems(FailureFolder.Id, sfilter, iv)

I still don't know how to find all emails from users at the same domain though.

More Info:

I really needed to filter by Sender Domain and did that by pulling the entire folder contents down and filtering in .Net code. Even that causes problems.

Basically to keep things quick and tight, I tried to pull all the data with a PropertySet:

New PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.Sender)

Filtering still didn't work, yet email addresses still showed in my list of items view. Well it turns out that the value of Message.Sender contains some kind of ActiveDirecty path in it until you call LoadPropertiesForItems. After LoadPropertiesForItems, it's an email address.

Note that my earlier attempt to filter at the server was scuppered because filtering would have to occur against the ActiveDirectory path style of string.

This is all highly confusing, and not at all user friendly.

If anybody has any idea on how to filter by email domain at the server, let me know!

Mark

Upvotes: 7

Jason Johnston
Jason Johnston

Reputation: 17692

What is your goal? Sender isn't a string property, so I'm not surprised that the results are odd with ContainsSubstring. I tried it against Office 365 and it worked, but older versions of Exchange may not be as "smart" about handling this kind of query. Depending on what you're trying to achieve, there may be a better filter.

Upvotes: 0

Related Questions