如何添加字符串" matchingMessage"列表?
这是我想在gridview上显示结果的唯一方法。 因为字符串matchMessage现在不在列表中,所以我使用的是email.matchingMessagePublic,这是我在我的类上声明的公共字符串,gridview上的结果与第一个结果的结果相同,这里是我的代码
private bool DisplayErrorEmails(ExchangeService service)
{
try
{
FindItemsResults<Item> findResults;
DateTime TwoDays = DateTime.Today.AddDays(-2);
//Search for the Undelivered Email
SearchFilter.SearchFilterCollection compoundFilter = new
SearchFilter.SearchFilterCollection(LogicalOperator.Or);
compoundFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass,
"REPORT.IPM.Note.NDR"));
compoundFilter.Add(new
SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.DR"));
compoundFilter.Add(new
SearchFilter.ContainsSubstring(ItemSchema.ItemClass,
"REPORT.IPM.Note.DELAYED"));
compoundFilter.Add(new
SearchFilter.ContainsSubstring(ItemSchema.ItemClass,
"REPORT.IPM.Note.IPNRN"));
compoundFilter.Add(new
SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.IPNNRN"));
compoundFilter.Add(new
SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.SMIME.NDR"));
compoundFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.SMIME.DR"));
compoundFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.NoteSMIME.MULTIPARTSIGNED.NDR"));
compoundFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.ItemClass, "REPORT.IPM.Note.SMIME.MULTIPARTSIGNED.DR"));
//Displays the result
ItemView view = new ItemView(100);
do
{
findResults = service.FindItems(WellKnownFolderName.Inbox, compoundFilter, view);
foreach (Item item in findResults.Items)
{
if (findResults.ToString() != "")
{
EmailList.Add(item);
item.Load();
string Sender = item.DisplayTo;
string Subject = item.Subject;
string DateCreated = item.DateTimeCreated.ToString();
//Display the EmailBody
PropertySet itemProperty = new PropertySet();
itemProperty.RequestedBodyType = BodyType.Text;
itemProperty.Add(ItemSchema.Body);
PropertySet FindItemPropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet = FindItemPropertySet;
PropertySet GetItemsPropertySet = new PropertySet(BasePropertySet.FirstClassProperties);
GetItemsPropertySet.RequestedBodyType = BodyType.Text;
if (findResults.Items.Count > 0)
{
service.LoadPropertiesForItems(findResults.Items, GetItemsPropertySet);
foreach (Item Item in findResults.Items)
{
var EmailBody = Item.Body.Text;
var pattern = new[] { "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",
"Remote Server returned '550 No Such User Here",
"5.2.3 smtp;550 5.2.3 RESOLVER.RST.SendSizeLimit.Org; message too large for this organization",
"5.1.10 smtp;550 5.1.10 RESOLVER.ADR.RecipientNotFound; Recipient user@contoso.com not found by SMTP address lookup",
"Remote Server returned '550 4.4.7 QUEUE.Expired; message expired'",
"Remote host said: 550-5.1.1 The email account that you tried to reach does not exist",};
string matchingMessage = String.Empty;
var found = pattern.Any(m => { var match = EmailBody.Contains(m);
if (match) matchingMessage = m;
return match;
});
matchingMessagePublic = matchingMessage;
Console.WriteLine("Subject" + item.Subject);
Console.WriteLine("Error::::" + matchingMessage);
}
}
}
}
if (findResults.NextPageOffset != null)
{
view.Offset = (int)findResults.NextPageOffset;
}
}
while (findResults.MoreAvailable);
Status = "list of undelivered email.";
return true;
}
catch (Exception ex)
{
Status = "Error: " + ex.Message;
return false;
}
}
这是我的代码显示到gridview
public void LoadResult()
{
EmailMethods email = new EmailMethods();
email.EmailServer = "https://SampleExchange.asmx";
email.AccountName = ConfigurationManager.AppSettings["Account"];
email.Password = ConfigurationManager.AppSettings["Secret"];
email.Domain = ConfigurationManager.AppSettings["Domain"];
email.GetEmails();
DataTable dt = new DataTable();
dt.Columns.Add("Subject", typeof(string));
dt.Columns.Add("Sender", typeof(string));
dt.Columns.Add("DateCreated", typeof(string));
dt.Columns.Add("EmailHeader", typeof(string));
dt.Columns.Add("ErrorTwo", typeof(string));
foreach (Item item in email.EmailList)
{
dt.Rows.Add(item.Subject, item.DisplayTo, item.DateTimeCreated, item.Body, email.matchingMessagePublic);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
在email.matchingMessagePublic上的它显示第一个结果,并在第一个结果中将网格填充为相同的结果。如果我可以将matchMessage添加到项目中,我认为它将正确显示结果。任何人都可以帮我解决我的代码
答案 0 :(得分:0)
创建一个包含所有匹配消息的公共列表。
foreach (Item item in email.EmailList)
{
dt.Rows.Add(item.Subject, item.DisplayTo, item.DateTimeCreated, item.Body, FindInMatchingMesage("ItemToLookFor"));
GridView1.DataSource = dt;
GridView1.DataBind();
}