2

I've create a small application that gets all my emails. I add each email to a list. However before I add them to a list I filter them. All of my filters are working apart from one. My search filter for filtering my email by sender is not working as I'm only trying to filter the domain and not the whole email address. For example xxx@xxx.com will filter however I want to filter out everything with xxx.com domain and for some reason it doesn't filter it. I tried using substring and this doesn't work either.

My code is as fallows

    private static SearchFilter.SearchFilterCollection sFilter = new SearchFilter.SearchFilterCollection();
     private static FindItemsResults<Item> findResults;
        sFilter.Add(new SearchFilter.Not(new SearchFilter.ContainsSubstring(EmailMessageSchema.Sender, "@xxx.com",ContainmentMode.Substring,ComparisonMode.IgnoreCase)));
        sFilter.Add(new SearchFilter.Not(new SearchFilter.Exists(EmailMessageSchema.InReplyTo)));
        DateTime startTime = GetDateValueforFilter();
        startTimefilter = new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeReceived, startTime);
sFilter.Add(startTimefilter);


            sFilter.Add(startTimefilter);


            findResults = service.FindItems(
                WellKnownFolderName.Inbox
                ,sFilter
                ,new ItemView(25));

            foreach (EmailMessage item in findResults.Items)
            {



                //if (item.IsRead == false)
                //{
                //    if (item.InReplyTo == null)
                //    {


                        bool replyToAll = true;
                        string myReply = "This is the message body of the email reply.";

                        item.Reply(myReply, replyToAll);

                        item.IsRead = true;
                        item.Send();


                    //}

                //}

            }
Craig Gallagher
  • 1,613
  • 6
  • 23
  • 52
  • read this: http://stackoverflow.com/questions/22991782/searching-exchange-mailbox-by-email-address-using-searchfiltercollection – Seabizkit Jan 27 '17 at 12:42
  • Possible duplicate of [searching Exchange mailbox by email address using SearchFilterCollection](http://stackoverflow.com/questions/22991782/searching-exchange-mailbox-by-email-address-using-searchfiltercollection) – Seabizkit Jan 27 '17 at 12:43
  • I don't want to use the the specific email address I only want the domain of the email e.g xxx.com instead of xxx@xxx.com. The example you provided would only filter an exact email address – Craig Gallagher Jan 27 '17 at 13:07
  • yes i see that, please also read the comments, they may assist. – Seabizkit Jan 27 '17 at 13:37

1 Answers1

3

I would suggest you try using the Extended Property PidTagSenderSmtpAddress https://msdn.microsoft.com/en-us/library/office/jj713594.aspx and try something like

  ExtendedPropertyDefinition PidTagSenderSmtpAddress = new ExtendedPropertyDefinition(0x5D01,MapiPropertyType.String);
  SearchFilter sf = new SearchFilter.ContainsSubstring(PidTagSenderSmtpAddress, "@yahoo.com");
Glen Scales
  • 20,495
  • 1
  • 20
  • 23