locked
EWS: Only get external mails from inbox RRS feed

  • Question

  • Hi,

    Is it possible to get only external mail messages (from outside the domain) from inbox using SearchFilters?

    How to do that?

    Best Regards

    Andreas 

    Tuesday, January 15, 2013 4:18 PM

Answers

  • You can use the PR_SENDER_ADDRTYPE extended property and create a search filter around that which should  give you the same result. eg

                ExtendedPropertyDefinition PR_SENDER_ADDRTYPE_W = new ExtendedPropertyDefinition(0x0C1E, MapiPropertyType.String);
                SearchFilter sf = new SearchFilter.IsEqualTo(PR_SENDER_ADDRTYPE_W, "EX");
                ItemView iv = new ItemView(1000);
                FindItemsResults<Item> fiResults = null;
                do
                {
                    fiResults = service.FindItems(WellKnownFolderName.Inbox, sf, iv);
                    foreach (Item itItem in fiResults.Items)
                    {
                        Console.WriteLine(itItem.Subject);
                    }
                    iv.Offset += fiResults.Items.Count;
                } while (fiResults.MoreAvailable == true);
    Cheers
    Glen
    Friday, January 18, 2013 6:13 AM

All replies

  • Hello Andreas,

    Off the top of my head, here is some code that shows how to perform a server-side filter for emails outside your domain. I added client-side filtering since internal senders may be identified by their legacyExchangeDN which may not contain the user's domain.  This may return up to five emails originating from outside your domain:

    static void FindExternalEmails(ExchangeService service, string myDomain) { SearchFilter.ContainsSubstring myDomainFilter = new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, myDomain); SearchFilter.Not notFilter = new SearchFilter.Not(myDomainFilter); ItemView view = new ItemView(5); view.PropertySet = new PropertySet(BasePropertySet.IdOnly, EmailMessageSchema.Subject, EmailMessageSchema.Sender); // Server-side filtering FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, notFilter, view); foreach (Item item in results) { if (item is EmailMessage) { EmailMessage email = (item as EmailMessage); // Client-side filtering.

    if (email.Sender.RoutingType == "SMTP") { Console.WriteLine("\n\rSender: " + email.Sender + "\n\rSubject: " + email.Subject); } } } }

    There might be a better way. I'm unsure how to filter server-side on routing type.

    With regards,

     

    Michael | Microsoft Exchange Developer Content

    The Exchange Development Forum Guide has useful information for using the Exchange Development Forum.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.


    Tuesday, January 15, 2013 6:59 PM
  • Hi Michael,

    thanks for your reply. What I search for is pure server-side filtering. Does anybody know how to filter by RoutingType server-side?

    Any other options?

    Best Regards

    Andreas

    Thursday, January 17, 2013 1:17 PM
  • You can use the PR_SENDER_ADDRTYPE extended property and create a search filter around that which should  give you the same result. eg

                ExtendedPropertyDefinition PR_SENDER_ADDRTYPE_W = new ExtendedPropertyDefinition(0x0C1E, MapiPropertyType.String);
                SearchFilter sf = new SearchFilter.IsEqualTo(PR_SENDER_ADDRTYPE_W, "EX");
                ItemView iv = new ItemView(1000);
                FindItemsResults<Item> fiResults = null;
                do
                {
                    fiResults = service.FindItems(WellKnownFolderName.Inbox, sf, iv);
                    foreach (Item itItem in fiResults.Items)
                    {
                        Console.WriteLine(itItem.Subject);
                    }
                    iv.Offset += fiResults.Items.Count;
                } while (fiResults.MoreAvailable == true);
    Cheers
    Glen
    Friday, January 18, 2013 6:13 AM
  • Hi Glen,

    thanks for your suggestion.

    Filtering to PR_SENDER_ADDRYPE_W by value "EX" brings me the internal mails (from within my Exchange environment). So I guess I have to filter for value "SMTP" to get the external mails, is that right?

    Best Regards

    Andreas

    Friday, January 18, 2013 8:51 AM
  • Yeah using SMTP would work, Also if you look at Michael's example he uses a Not SearchFilter which essentially negates the search term (eg Not "EX"). That maybe a better use because it would include any Gateway address types if your using them (Eg sometime third party FAX server products might use there own addresstypes).

    Cheers
    Glen

    Monday, January 21, 2013 3:42 AM
  • Alright - thanks for your help!

    Best Regards

    Andreas

    Monday, January 21, 2013 8:38 AM
  • Hi,

    I've got another question concerning this topic:

    It's now clear to me how I can filter for incoming messages from internal/external senders, but how about the other way?

    How can I get only items from 'Sent items' with internal/external recipients? I could not find any property (like PR_SENDER_ADDRTYPE for incoming mails)... Do I have to filter that on client side by processing all recipients addresses and check the RoutingType for each address?

    How does the built in Exchange Journaling feature detect internal/external mails (sent mails)?

    Any ideas?

    Best Regards

    Andreas

    Thursday, September 26, 2013 4:15 PM
  • Journaling is done via a Transport Agent in the Transport Pipeline so its completely different from using EWS.

    Messages your sending can have one or more recipients some of whom maybe Internal or External. So all you can do with EWS is check the To and CC recipient properties and look at the domains of the recipients (EWS will always return the SMTPAddress for Rcpts).

    Cheers
    Glen

    Friday, September 27, 2013 4:44 AM