我正在尝试获取我在搜索过滤器上声明的所有来自EWS的电子邮件,在控制台中显示所有记录,但当我尝试将其查看到具有gridview的网页时,它只显示第一个记录并显示超过20+的gridview。这是我的代码:
protected void Button1_Click(object sender, EventArgs e)
{
EmailMethods email = new EmailMethods();
email.EmailServer = "https://SampleEWSEmail.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("DateSent", typeof(string));
dt.Columns.Add("DateReceive", typeof(string));
foreach (string item in email.EmailList)
{
dt.Rows.Add(email.Subject, email.Sender, email.DateSent, email.DateReceive);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
public void GetEmails()
{
if (EmailServer != "" && AccountName != "" && Password != "")
{
try
{
EmailList = new List<string>();
ServicePointManager.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
service.Credentials = new WebCredentials(AccountName, Password, Domain);
service.Url = new Uri(EmailServer);
DisplayErrorEmails(service);
}
catch (Exception ex)
{
Status = "Error: " + ex.Message;
}
}
else
{
Status = "Please provide the Email Server, Account Name and Password.";
}
}
private bool DisplayErrorEmails(ExchangeService service)
{
try
{
FindItemsResults<item> findResults;
SearchFilter.SearchFilterCollection compoundFilter =
new SearchFilter.SearchFilterCollection(LogicalOperator.Or);
compoundFilter.Add(new SearchFilter.ContainsSubstring(ItemSchema.Body, "Sorry, I couldn't find any host named", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The server is unable to connect", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Your outgoing message timed out because of issues concerning the incoming server.", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Invalid characters", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Access denied, spam abuse detected", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Access denied, banned sender", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The email account that you tried to reach does not exist", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The domain name in the email address is incorrect", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Host unknown or Domain lookup failed", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Mailbox full or Quota exceeded", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Mailbox unavailable", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Mailbox not found, Invalid mailbox, or User unknown", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "A time-out occurred while communicating with the server", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Timeout connection problem: there have been issues during the message transfer", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Connection refused or inability to open an SMTP stream", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The server cannot verify the user, but it will try to deliver the message anyway", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The service is unavailable due to a connection problem: it may refer to an exceeded limit of simultaneous connections, or a more general temporary problem", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The recipient's mailbox has exceeded its storage limi", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The recipient's Exchange Server incoming mail queue has been stopped", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The recipient's server is not responding", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The connection was dropped during the transmission", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The maximum hop count was exceeded for the message: an internal loop has occurred", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "A routing error", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Your ISP's server or the server that got a first relay from yours has encountered a connection problem", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Too many emails sent or too many recipients: more in general, a server storage limit exceeded", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "An error of your mail server, often due to an issue of the local anti-spam filter", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "A syntax error: the server couldn't recognize the command", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The command is not implemented", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The server has encountered a bad sequence of commands, or it requires an authentication", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "A command parameter is not implemented", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Bad email address", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "A DNS error: the host server for the recipient's domain name cannot be found", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Address type is incorrect: another problem concerning address misspelling.In few cases, however, it's related to an authentication issue", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The total size of your mailing exceeds the recipient server's limits", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "A problem occurred while delivering this message to this email address", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "This message hasn't been delivered yet", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The server could not be found", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Outlook is unable to connect to your outgoing (SMTP) e-mail server", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "The operation timed out waiting for a response from the receiving (POP) server", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "A time-out occurred while communicating with the server", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Delivery has failed to these recipients or groups", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
compoundFilter.Add(new SearchFilter.ContainsSubstring(EmailMessageSchema.Body, "Delivery is delayed to these recipients or groups", ContainmentMode.ExactPhrase, ComparisonMode.IgnoreCaseAndNonSpacingCharacters));
MailMessage msg = new MailMessage();
//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(Subject = item.Subject.ToString());
EmailList.Add(Sender = item.DisplayTo.ToString());
EmailList.Add(DateReceive = item.DateTimeReceived.ToString());
EmailList.Add(DateSent = item.DateTimeSent.ToString());
}
}
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上显示的示例记录:
Subject Email Receiver Date and Time Sent Date and Time Received
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM
Undeliverable: crm fdgdfgfdgf@fsfa.com 11/30/2017 9:15:11 PM 11/30/2017 9:15:11 PM