gilly3
gilly3

Reputation: 91487

Find email by sender email address - Filter syntax

In an Outlook 2010 Add-in, I want to find an item in a folder sent by a given email address:

MailItem FindEmailFromSender(MAPIFolder folder, string emailAddress)
{
    string filter = "[sender] = '" + emailAddress + "'"; // This filter is wrong.
    return folder.Items.Find(filter) as MailItem;
}

It seems like this should be super easy, but I just cannot find the correct syntax. What is the correct filter syntax for filtering on sender email address?

Upvotes: 4

Views: 5718

Answers (3)

gilly3
gilly3

Reputation: 91487

Thanks to nemesv for the answer above. I also managed to figure out how to find the sender address when an email is sent "on behalf of" another account thanks to this answer on another question. I'm including it here for completeness:

MailItem FindEmailSentOnBehalfSender(MAPIFolder folder, string emailAddress)
{
    string filter = "@SQL=\"http://schemas.microsoft.com/mapi/proptag/0x0065001F\" = '{0}'";
    return folder.Items.Find(string.Format(filter, emailAddress)) as MailItem;
}

Upvotes: 1

nemesv
nemesv

Reputation: 139758

Based on the MailItem documentation you need to use

[SenderEmailAddress] for the e-mail address of the sender

or

[SenderName] for the display name of the sender

You should also note that the property names are case sensitive.

Upvotes: 3

SliverNinja - MSFT
SliverNinja - MSFT

Reputation: 31641

You are probably looking for E-mail Account property filter. This example worked for me...

string filter = "[E-mail Account] = '" + emailAddress + "'"; // filters on exact address
return folder.Items.Find(filter) as MailItem;

If you want to search by sender name, you can use this:

string filter = "[From] = '" + senderName + "'"; // filters on exact sender name "Bob Johns"
return folder.Items.Find(filter) as MailItem;

Also see MSDN for item filtering reference and this MSDN blog post describing how to find DASL property names using Advanced Find.

Upvotes: 1

Related Questions