Search code examples
c#outlooknetoffice

Outlook Filter Messages by CC (C#, NetOffice)


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


Solution

  • 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