Gokul Gokul - 3 years ago 82
C# Question

To filter mails from inbox which are sent to another recipient?

In Outlook Interop,

using Outlook = Microsoft.Office.Interop.Outlook;

To filter unread emails, I'm using the following code

Outlook.Application oApp = new Outlook.Application();
Outlook.NameSpace outlookNameSpace = oApp.Application.GetNamespace("MAPI");
Outlook.MAPIFolder inbox = outlookNameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
Outlook.Items items = inbox.Items.Restrict("[Unread]=true");

Likewise, Is there a way to filter the mails sent to a specific recipient other than me.

Say, filter mails sent to

My mail ID would also be present in those mails in To or in CC. I will be happier to find a possible way with Outlook interop.

Answer Source

You can use a search query like the following to search for a match on the PR_DISPLAY_TO MAPI property (replace 0x0E04001F with 0x0E03001F for PR_DISPLAT_CC)

@SQL="http://schemas.microsoft.com/mapi/proptag/0x0E04001F" LIKE '%abc@example.com%'

Keep in mind however that PR_DISPLAT_TO / PR_DISPLAY_CC may or may not contain the email addresses; they could just contain display names.

On the Extended MAPI level (C++ or Delphi), you can create a restriction on recipients (RES_SUBRESTRICTION / PR_MESSAGE_RECIPIENTS).

If using Redemption (any language) is an option, you can use RDOFolder.Items.Restrict - unlike Outlook Object Model, it does expand To/CC/BCC queries into recipient sub restrictions on PR_DISPLAY_NAME and PR_EMAIL_ADDRESS properties on each recipient (RES_SUBRESTRICTION / PR_MESSAGE_RECIPIENTS / RES_OR / PR_DISPLAY_NAME | PR_EMAIL_ADDRESS).

set Session = CreateObject("Redemption.RDOSession")
Session.MAPIOBJECT = Application.Session.MAPIOBJECT
set Folder = Session.GetFolderFromID(Application.ActiveExplorer.CurrentFolder.EntryID)
set restrItems = Folder.Items.Restrict(" TO = 'abc@example.com' ")

You can also specify Recipients property in aquery - it will be matched against recipients of all types (to/cc/bb):

set restrItems = Folder.Items.Restrict(" Recipients = 'abc@example.com' ")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download