Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to add the string "matchingMessage" to the List<Item>?

this is the only way that i think in order to display the result on the gridview. since the string matchMessage is not part on the list for now im using the email.matchingMessagePublic that is a public string that i declare on my class and the result on the gridview is only the same on the first result and here's my code



C#
<pre>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;
    }
}

and here's my code to display to the gridview


<pre lang="c#">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();

    }

}


What I have tried:

on the email.matchingMessagePublic it display the first result and it fill the grid as a same result on the first one. if maybe i add the matchMessage to the item i think it will display the result correctly. Can anyone help me with my code please
Posted
Updated 5-Jan-18 15:25pm
v3
Comments
ZurdoDev 5-Jan-18 13:34pm    
I have no idea what you are asking.
Member 13427032 5-Jan-18 13:56pm    
can i add the sting matchingMessage to the List<item>? because my on the
dt.Rows.Add(item.Subject, item.DisplayTo, item.DateTimeCreated, item.Body, email.matchingMessagePublic);
to display the result on the gridview on the email.matchingMessagePublic it display only the same record so i tried to add the matchingMessage to the List<item> so that i can display the correct result for now i declare a public string matchingMessagePublic to the emailMethods class. Cna you help me with my code please

1 solution

try this

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",};

foreach (Item item in email.EmailList)
{
    string matchingMessage = pattern.FirstOrDefault(k => item.Body.Contains(k));
    dt.Rows.Add(item.Subject, item.DisplayTo, item.DateTimeCreated, item.Body, matchingMessage);

}
GridView1.DataSource = dt;   // have moved this outside, else it will called again and again
GridView1.DataBind();         // which will result in stack over flow exception when huge records
 
Share this answer
 
Comments
Member 13427032 6-Jan-18 7:35am    
thanks for the solution. I'll bbe working on that
Member 13427032 6-Jan-18 9:32am    
this solution is working thank you so much sir
Karthik_Mahalingam 6-Jan-18 9:44am    
Welcome

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