How Exchange IMAP Search in message header
In case you or your script needs to search in message headers during IMAP session with
Exchange, please keep in mind next nuances:
To SEARCH for something in message header we can use SEARCH or SEARCH HEADER
If you use SEARCH FROM (for CC, BCC, TO idea the same) Exchange will search in message's Property: PR_SENT_REPRESENTING_NAME, PR_SENT_REPRESENTING_NAME_A,
ptagSentRepresentingName
The PR_SENT_REPRESENTING_NAME property contains the display name for the messaging user represented by the sender.
As fact, it is only name, not e-mail address
So if you receive message from me, you will see in P2 header:
From: Dmitry Khrebin dkhrebin@microsoft.com
Result is that ptagSentRepresentingName = Dmitry Khrebin
SMTP address stored in the separate Property: PidTagSenderSmtpAddress, ptagSenderSmtpAddress
And as a result you cannot find my message if try to search by e-mail address or by alias.
Only exception here – if no representing name in P2 header at all, like:
From: dkhrebin@microsoft.com
In this case, Exchange will copy e-mail address to ptagSentRepresentingName
The outgoing transport provider must always leave PR_SENT_REPRE represented sender properties unset.SENTING_NAME unchanged if it has been set by the sending client. If it is unset, the transport provider should set it to PR_SENDER_NAME on the outbound copy of the message, and leave it unset on the local copy.
Okey, what shall we do if we works with general messages with sender name in it?
Just use SEARCH HEADER
How it works:
SEARCH HEADER searches in Property: PR_TRANSPORT_MESSAGE_HEADERS, PR_TRANSPORT_MESSAGE_HEADERS_A
It is exactly what you can see in Outlook when open Message properties and look for Internet Headers
If we ask IMAP to SEARCH HEADER <field-name> <string> it will looks for
"\n" + fieldName + ":" + "*string*"
("\n" just to be sure that we look for beginning of the string to avoid false positive reaction in case somebody wrote in subject From: user@domain.com)