Outlook Filter Messages by CC (C#, NetOffice)

66 views Asked by At

I need to get messages that have a specific address in the CC field. I've tried to get messages this way:

Outlook.Folder folder = (Outlook.Folder) this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);

string cc = "[email protected]";

string filter = @"@SQL=(""urn:schemas:httpmail:cc"" LIKE '%" + cc + @"')";

Outlook.Items restrictedItems = folder.Items.Restrict(filter);

In this way all messages with addresses that are displayed in Outlook itself in the "CC" field as addresses are found. But the problem is that this way does not find messages whose addresses are not directly displayed (as on the screenshot):enter image description here

I also tried to compose a query with "urn:schemas:httpmail:displaycc", but no luck either. Is there any way to write a filter so that I can only get messages that have a specific address in the "CC" field?

For example, I had a similar problem when filtering messages by sender. This article http://philliphoff.github.io/finding-dasl-property-names/# helped me. Thanks to it, I added "http://schemas.microsoft.com/mapi/proptag/0x0065001f" and "http://schemas.microsoft.com/mapi/proptag/0x0042001f" to my query and everything worked fine

2

There are 2 answers

0
Alldman On BEST ANSWER

The real CC address (same for the recipient) was only obtained here:

const string PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001F";
using var propAccessor = mailItem.PropertyAccessor;

var messageHeaders = propAccessor.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS).ToString();

You can scroll through the list of messages and look for the presence of the desired address in them. I don't think this is the most optimal method, but I managed to solve the problem this way. I have not found any other solution

2
Dmitry Streblechenko On

That is correct - OOM only allows to search on the properties on the message itself (such as PR_DISPLAY_CC), it does not create queries on message subobjects, such as recipients or attachments, even though MAPI allows it (RES_SUBRESTRICTION with a restriction on PR_MESSAGE_RECIPIENTS or PR_MESSAGE_ATTACHMENTS.

If using Redemption (I am its author) is an option, it does create a restriction on PR_MESSAGE_RECIPIENTS with a match on PR_DISPLAY_NAME / PR_EMAIL_ADDRESS / PR_SMTP_ADDRESS / PR_RECIPIENT_TYPE == MAPI_CC when you specify CC in a search query.

Off the top of my head (VBA):

set outlookFolder = Application.ActiveExplorer.CurrentFolder

Set redemptionSession = CreateObject("Redemption.RDOSession")
redemptionSession.MAPIOBJECT = Application.Session.MAPIOBJECT
set redemptionFolder = redemptionSession.GetFolderFromID(outlookFolder.EntryID)
set restrictedItems = redemptionFolder.Items.Restrict(" CC = '[email protected]' ")
for each item in restrictedItems
  Debug.Print item.Subject
next