Home > Back-end >  Outlook.MAPIFolder.Items.Find returning null when domain has a number in it?
Outlook.MAPIFolder.Items.Find returning null when domain has a number in it?

Time:02-15

I'm trying to query an outlook folder for emails that have a specific string in the sender's address. My current query looks like the below:

"@SQL=\"urn:schemas:httpmail:fromemail\" like '%isc2.org%'"

This works with just about every domain I throw at it but for the above specifically it balks and return None. I've already inspected the header of the email to verify that the FROM is for sure from ics2.org and not mailed on behalf or anything like that. The searches are originating from an add-in that has an email open and queries its sender info and to determine what folder to search based on the email's parent MAPIFolder; so there's no chance that the folder doesn't have any emails from this domain because I had to physically open one to initiate the action by the plugin that uses this query. Here's a code-snippet of what's going on with the query as well:

var folder = mailItem.Parent as Outlook.MAPIFolder;
string address = mailItem.Sender.Address;
if (mailItem.SenderEmailAddress != null && address != mailItem.SenderEmailAddress)
{
    address = mailItem.SenderEmailAddress;
}
filter = "@SQL=\"urn:schemas:httpmail:fromemail\""
           " like  \'%"   address   "%\'";
var resultItem = folder .Find(filter);
while(resultItem != null){
    ...do stuff...
    resultItem = folderItems.FindNext();
}

To be fair as well this is just my hypothesis in terms of numbers in the domain being an issue with queries like this, but haven't tripped across another domain in my email that has numbers in it to get more evidence supporting that theory. Any advice in general is appreciated!

CodePudding user response:

Try to use the urn:schemas:httpmail:sender property instead:

filter = "@SQL=\"urn:schemas:httpmail:sender\"
           " like  \'%"   address   "%\'";

or

filter = "@SQL=\"urn:schemas:httpmail:sender\"
           " like  \"%"   address   "%\"";

Also you try using the phrase matching operator:

filter = "@SQL=\"urn:schemas:httpmail:sender\"
           " ci_phrasematch \'%"   address   "%\";

If it doesn't work I'd suggest playing with filter setting in Outlook manually. Then you can find the required query which you can apply programmatically for filtering generated by Outlook for you.

BTW Outlook generates the following condition:

filter = "@SQL=\"urn:schemas:httpmail:sender\"
           " LIKE \'%"   address   "%\'";

CodePudding user response:

I manually modified PR_SENT_REPRESENTING_EMAIL_ADDRESS property to "[email protected]" on one of my messages and the following query worked fine for me:

@SQL="http://schemas.microsoft.com/mapi/proptag/0x0065001F" like '%isc2.org%'

0x0065001F is PR_SENT_REPRESENTING_EMAIL_ADDRESS

  • Related