Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on the EWS to get all of the undelivered email and on the console it show up all of the undelivered email but on the gridview it show up only one record and that record was the first one. And here's my code. Please help me with my work.
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();


       }
   }


What I have tried:

And here's the code to get all of the undelivered email.
<pre> 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.";
            }

        }

<pre lang="c#"><pre> 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());
                            
                            Console.WriteLine("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;
            }
        }
Posted
Updated 10-Dec-17 11:03am
Comments
F-ES Sitecore 10-Dec-17 9:47am    
What happens when you step through the code in the debugger? How many items are in email.EmailList? How many rows do you end up with in dt?
Member 13427032 10-Dec-17 10:29am    
it count all of the result when i debug it and it shows more than 20+ rows with the same record and that is the first record.
Member 13427032 10-Dec-17 10:34am    
it just like rotating on the first record and it not just going up on the other records.
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

that is showing on the gridview and that is 20+ in total
Dave Kreskowiak 10-Dec-17 10:31am    
Simply the search filter and see what you get. That filter is, shall we say, horrible in its complexity and coding. That's the first place I'd look after seeing exactly what it's returning. Knock it down to only one condition to see what you get, then add one and try again.
Member 13427032 10-Dec-17 10:42am    
ok thanks i'll try that.

1 solution

This code isn't doing what you think it is

EmailList.Add(Subject = item.Subject.ToString());
                            
Console.WriteLine("Subject: " + item.Subject.ToString());
EmailList.Add(Sender = item.DisplayTo.ToString());
EmailList.Add(DateReceive = item.DateTimeReceived.ToString());
EmailList.Add(DateSent = item.DateTimeSent.ToString());


The first line is setting the "Subject" variable to be item.Subject and adding the result of that variable to the list. The same with Sender etc. If you look at EmailList it will be a list of strings like

"subject 1"
"sender 1"
"date r 1"
"date s 1"
"subject 2"
"sender 2"
"date r 2"
"date s 2"

In your loop when you're adding this to the datatable

foreach (string item in email.EmailList)
{
               
    dt.Rows.Add(email.Subject, email.Sender, email.DateSent, email.DateReceive);
}


You're getting the string from EmailList into "item" but not doing anything with it. Instead you're adding "email.Subject" etc, and as discussed above email.Subject is going to be the last subject you added when updating EmailList as your loop is updating the Subject variable with each iteration leaving it with the last value updated in your loop.

Instead what you're probably looking to do is to add "item" itself in your first loop

foreach (Item item in findResults.Items)
{
    if (findResults.ToString() != "")
    {
        EmailList.Add(item);
    }
}


Then update your other loop

foreach (Item item in email.EmailList)
{
    dt.Rows.Add(item.Subject, item.DisplayTo.ToString(), item.DateSent.ToString(), email.DateReceive.ToString());
}
 
Share this answer
 
Comments
Member 13427032 14-Dec-17 11:06am    
when i tried to add EmailList.Add(item); and also update on this (Item item in email.EmailList) i got an erro cannot convert from 'Microsot.Exchange.WebServices.Data.Item' to string
F-ES Sitecore 14-Dec-17 11:41am    
You need to change EmailList from List<string> to List<Item>
Member 13427032 14-Dec-17 12:01pm    
Thank you so much for the help, im just new here web developing hehehe. Can i have a suggestion to improve my skill on web developing? thank you so much and God Bless
F-ES Sitecore 14-Dec-17 12:09pm    
You just learn by doing really, it takes time but every problem you meet and overcome is knowledge learned.
Member 13427032 14-Dec-17 12:02pm    
i mean what would you suggest to improve my skills in programming

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900