如何在电子邮件正文中搜索多行文字?

时间:2017-12-30 19:06:18

标签: c# regex exchangewebservices

如何搜索多行文字到

这样的电子邮件正文

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。这是我的代码,用于显示未送达或退回电子邮件的正文:

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);
}

任何人都可以帮助我或者提前告诉我如何在电子邮件正文中搜索并提前感谢新年快乐

1 个答案:

答案 0 :(得分:0)

如果你可以假设你的短语没有被划分,你可以只查找它们:

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));

如果它们可能被分解,您可以使用Regex并处理所有空格:

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));

如果它们可以在单词的中间划分,那就更难了,但我认为这不太可能。

要返回匹配的消息,您可以执行以下操作:

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

然后foundMessages.Count将告诉您是否找到了任何内容。