Search code examples
c#regexexchangewebservices

How to search multiple lines of words to the body of email?


how to search multiple lines of words to an email body like

Remote Server returned '550 No Such User Here', Remote Server returned '554 5.4.4 SMTPSEND.DNS.NonExistentDomain; nonexistent domain', or Either there are no alternate hosts, or delivery failed to all alternate hosts. Here is my code to display the body of the undelivered or bounce back emails:

private static void DisplayBody(ExchangeService service)
{
        PropertySet itemProperty = new PropertySet();
        itemProperty.RequestedBodyType = BodyType.Text;
        itemProperty.Add(ItemSchema.Body);
        SearchFilter.SearchFilterCollection searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.Or, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.NDR"));
        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.DR"));
        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.DELAYED"));
        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.IPNRN"));
        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.IPNNRN"));
        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.SMIME.NDR"));
        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.SMIME.DR"));
        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.NoteSMIME.MULTIPARTSIGNED.NDR"));
        searchFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.SMIME.MULTIPARTSIGNED.DR"));
        PropertySet FindItemPropertySet = new PropertySet(BasePropertySet.IdOnly);

        ItemView view = new ItemView(999);
        view.PropertySet = FindItemPropertySet;
        PropertySet GetItemsPropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
        GetItemsPropertySet.RequestedBodyType = BodyType.Text;

        FindItemsResults<Item> findResults = null;

        do
        {
            findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);
            if (findResults.Items.Count > 0)
            {
                service.LoadPropertiesForItems(findResults.Items, GetItemsPropertySet);
                foreach (Item Item in findResults.Items)
                {
                    string EmailHeader = Item.Body.Text;
                    Console.WriteLine(Item.Body.Text);
                }
            }
        } while (findResults.MoreAvailable);
}

can anyone help me or give me a hint on how to search on the body of an email thanks in advance and Happy New Year


Solution

  • If you can assume your phrases are not broken up across lines, you can just look for them:

    var EmailBody = Item.Body.Text;
    var messages = new[] {
        "Remote Server returned '550 No Such User Here'",
        "Remote Server returned '554 5.4.4 SMTPSEND.DNS.NonExistentDomain; nonexistent domain'",
        "Either there are no alternate hosts, or delivery failed to all alternate hosts." };
    
    var found = messages.Any(m => EmailBody.Contains(m));
    

    If it is possible they might be broken up, you can use Regex and handle all whitespace:

    var EmailBody = Item.Body.Text;
    var messages = (new[] {
        "Remote Server returned '550 No Such User Here'",
        "Remote Server returned '554 5.4.4 SMTPSEND.DNS.NonExistentDomain; nonexistent domain'",
        "Either there are no alternate hosts, or delivery failed to all alternate hosts." })
        .Select(m => Regex.Escape(m).Replace(@"\ ",@"\s+"))
        .ToArray();
    
    
    var found = messages.Any(m => Regex.IsMatch(EmailBody, m));
    

    If they can be broken across lines in the middle of words, it is a bit harder, but I assume that is unlikely.

    To return the matching message(s), you can do something like this:

    var foundMessages = messages.Where(m => EmailBody.Contains(m)).ToList();
    

    Then foundMessages.Count will tell you if any were found.