Ejemplo n.º 1
0
 public void ThreadWork()
 {
     try
     {
         // Replace the following line with your code.
         WriteLog("Start download email process");
         //  Array.ForEach(Directory.GetFiles(System.Configuration.ConfigurationSettings.AppSettings["MailPath"]), File.Delete);
         OpreateData opftp = new OpreateData();
         string strLog = opftp.ProcessData();
         if (strLog == "")
             WriteLog("End download email process");
         else
             WriteLog("End download email process " + strLog);
     }
     catch (ThreadAbortException abortException)
     {
         WriteLog("Download email process thread state: " + (string)abortException.ExceptionState + " error (" + abortException.Message + ")");
     }
     catch (Exception ex)
     {
         WriteLog("End download email process with error (" + ex.Message + ")");
     }
     finally
     {
         _running = false;
         connectionSuccess = true;
         WriteLog("_running is made false and connectionSuccess is made true");
     }
 }
Ejemplo n.º 2
0
        public string ProcessData()
        {
            string strLog = "";
            try
            {
                OpreateData opftp = new OpreateData();
                string strLogs = opftp.GetDataFromServer(ref connectionSuccess);//, ref index);

                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                XmlDocument xmlDoc = new XmlDocument();

                foreach (string file in Directory.EnumerateFiles(System.Configuration.ConfigurationSettings.AppSettings["MailPath"], "*.htm"))
                {
                    filename = Path.GetFileName(file);
                    XmlNode rootElement = xmlDoc.CreateElement("Root");
                    xmlDoc.AppendChild(rootElement);
                    htmlDoc.Load(file);
                    htmlDoc.OptionWriteEmptyNodes = true;
                    htmlDoc.OptionOutputAsXml = true;
                    string value = "";

                    HtmlNodeCollection nodesitemheading = htmlDoc.DocumentNode.SelectNodes(".//td[@class='content']");

                    XmlElement FileNameElement = xmlDoc.CreateElement("FileName");
                    FileNameElement.InnerText = Path.GetFileName(file);
                    rootElement.AppendChild(FileNameElement);
                    string strfile = file.Replace("htm", "txt");
                    string[] lines = File.ReadAllLines(strfile);

                    string From = lines[0].ToString().Replace("From:", "");
                    XmlElement FromElement = xmlDoc.CreateElement("From");
                    FromElement.InnerText = From;
                    rootElement.AppendChild(FromElement);

                    string SentDate = lines[1].ToString().Replace("SentDate:", "");
                    XmlElement SentDateElement = xmlDoc.CreateElement("SentDate");
                    SentDateElement.InnerText = SentDate;
                    rootElement.AppendChild(SentDateElement);

                    string Subject = lines[2].ToString().Replace("Subject:", "");
                    XmlElement SubjectElement = xmlDoc.CreateElement("Subject");
                    SubjectElement.InnerText = Subject;
                    rootElement.AppendChild(SubjectElement);

                    foreach (HtmlNode childNode in nodesitemheading)
                    {
                        if (childNode.ChildNodes.Count > 0)
                        {
                            foreach (HtmlNode childOfChildNode in childNode.ChildNodes)
                            {
                                if (childOfChildNode.Name == "#text")
                                {
                                    if (childOfChildNode.ChildNodes.Count > 0)
                                    {

                                        foreach (HtmlNode childOfChildofChildNode in childOfChildNode.ChildNodes)
                                        {

                                            if (childOfChildofChildNode.InnerText != "" && childOfChildofChildNode.InnerText != " " && childOfChildofChildNode.InnerText != "\r\n  ")
                                            {

                                                if (childOfChildofChildNode.InnerText.Trim().Contains("This is an order"))
                                                {
                                                    XmlElement Element = xmlDoc.CreateElement("PrimaryContact");
                                                    String[] SplitValue = Splits(sep(childOfChildofChildNode.InnerText, "from "), new char[] { ' ' });
                                                    string str1 = SplitValue[0] + " " + SplitValue[1];//primary contact
                                                    string str2 = SplitValue[3] + " " + SplitValue[4] + " " + SplitValue[5];//company name
                                                    str2 = str2.Replace("Relating", "");
                                                    string str3 = SplitValue[7] + " " + SplitValue[8] + " " + SplitValue[9] + " " + SplitValue[10] + SplitValue[11];//email body subject

                                                    XmlElement PrimaryContactElement = xmlDoc.CreateElement("PrimaryContact");
                                                    PrimaryContactElement.InnerText = str1;
                                                    rootElement.AppendChild(PrimaryContactElement);

                                                    XmlElement CompanyNameElement = xmlDoc.CreateElement("CompanyName");
                                                    CompanyNameElement.InnerText = str2;
                                                    rootElement.AppendChild(CompanyNameElement);

                                                    XmlElement EmailBodySubjectElement = xmlDoc.CreateElement("EmailBodySubject");
                                                    EmailBodySubjectElement.InnerText = str3;
                                                    rootElement.AppendChild(EmailBodySubjectElement);

                                                    XmlElement PropertyNameElement = xmlDoc.CreateElement("PropertyName");
                                                    str3 = str3.Replace("campaign:", "");
                                                    PropertyNameElement.InnerText = str3;
                                                    rootElement.AppendChild(PropertyNameElement);
                                                    //MessageBox.Show(str1 + ' ' + str2);
                                                }

                                                if (childOfChildofChildNode.InnerText.Trim().Contains("Property ID"))
                                                {
                                                    //MessageBox.Show(childOfChildofChildNode.InnerText.Trim().Replace("Property ID:", ""));//Property Id
                                                    XmlElement PropertyIdElement = xmlDoc.CreateElement("PropertyId");
                                                    PropertyIdElement.InnerText = childOfChildofChildNode.InnerText.Trim().Replace("Property ID:", "");
                                                    rootElement.AppendChild(PropertyIdElement);
                                                }

                                                if (childOfChildofChildNode.InnerText.Trim().Contains("OrderItem"))
                                                {
                                                    // MessageBox.Show(childOfChildofChildNode.InnerText.Trim().Replace("OrderItem:", ""));
                                                    String[] SplitValue = Splits(childOfChildofChildNode.InnerText.Trim().Replace("OrderItem:", ""), new char[] { ',' });//Order Item Id
                                                    SplitValueOrderItemId = Splits(childOfChildofChildNode.InnerText.Trim().Replace("OrderItem:", ""), new char[] { ',' });
                                                    XmlElement OrderItemIdElement = xmlDoc.CreateElement("OrderItemId");
                                                    for (int i = 0; i < SplitValue.Length; i++)
                                                    {
                                                        string str = SplitValue[i];
                                                        XmlElement IdElement = xmlDoc.CreateElement("Id");
                                                        IdElement.InnerText = str;
                                                        OrderItemIdElement.AppendChild(IdElement);
                                                    }
                                                    rootElement.AppendChild(OrderItemIdElement);
                                                }

                                                if (childOfChildofChildNode.InnerText.Trim().Contains("Sales contacts"))
                                                {
                                                    // MessageBox.Show(childOfChildofChildNode.InnerText.Trim().Replace("Sales contacts:", ""), "8");
                                                    String[] SplitValue = Splits(childOfChildofChildNode.InnerText.Trim().Replace("Sales contacts:", ""), new char[] { ',' });
                                                    for (int i = 0; i < SplitValue.Length; i++)
                                                    {
                                                        string str = (SplitValue[i].Replace(" on", ","));
                                                        String[] strValue = Splits(str, new char[] { ',' });

                                                        string str1 = strValue[0];//Sales contact name
                                                        string str2 = strValue[1].Replace("or", "");//Sales contact phone number

                                                        XmlElement SalesContactElement = xmlDoc.CreateElement("SalesContact");
                                                        SalesContactElement.InnerText = str1;
                                                        rootElement.AppendChild(SalesContactElement);

                                                        XmlElement SalesContactNumberElement = xmlDoc.CreateElement("SalesContactNumber");
                                                        SalesContactNumberElement.InnerText = str2;
                                                        rootElement.AppendChild(SalesContactNumberElement);
                                                    }

                                                    HtmlNodeCollection adminemail = htmlDoc.DocumentNode.SelectNodes(".//a[@href]");
                                                    string stradmin = adminemail[1].InnerText;//Admin email address

                                                    XmlElement AdminContactEmailElement = xmlDoc.CreateElement("AdminContactEmail");
                                                    AdminContactEmailElement.InnerText = stradmin;
                                                    rootElement.AppendChild(AdminContactEmailElement);
                                                }

                                                if (childOfChildofChildNode.InnerText.Trim().Contains("Admin contact"))
                                                {
                                                    // MessageBox.Show(childOfChildofChildNode.InnerText.Trim(), "9");
                                                    String[] SplitValue = Splits(childOfChildofChildNode.InnerText.Trim().Replace("Admin contact:", "").Replace(" on", ","), new char[] { ',' });

                                                    string str1 = SplitValue[0];//Admin contact name
                                                    string str2 = SplitValue[1].Replace("or", "");//Admin contact phone number

                                                    XmlElement AdminContactElement = xmlDoc.CreateElement("AdminContact");
                                                    AdminContactElement.InnerText = str1;
                                                    rootElement.AppendChild(AdminContactElement);

                                                    XmlElement AdminContactNumberElement = xmlDoc.CreateElement("AdminContactNumber");
                                                    AdminContactNumberElement.InnerText = str2;
                                                    rootElement.AppendChild(AdminContactNumberElement);

                                                }

                                                if (childOfChildofChildNode.InnerText.Trim().Contains("Supplier:"))
                                                {
                                                    //MessageBox.Show(childOfChildofChildNode.InnerText.Trim(), "10");
                                                    string str1 = childOfChildofChildNode.InnerText.Replace("Supplier:", "");//Product

                                                    XmlElement SupplierElement = xmlDoc.CreateElement("Supplier");
                                                    SupplierElement.InnerText = str1;
                                                    rootElement.AppendChild(SupplierElement);
                                                }

                                                if (childOfChildofChildNode.InnerText.Trim().Contains("Description of product/service being ordered:"))
                                                {
                                                    value = "Description of product";
                                                    continue;
                                                }

                                                if (value == "Description of product")
                                                {
                                                    //MessageBox.Show(childOfChildofChildNode.InnerText.Trim(), "11");
                                                    string str = childOfChildofChildNode.InnerText.Replace("Description of product/service being ordered: ", "");//Product Description
                                                    value = "";

                                                    XmlElement DescriptionElement = xmlDoc.CreateElement("Description");
                                                    DescriptionElement.InnerText = str;
                                                    rootElement.AppendChild(DescriptionElement);
                                                }

                                                if (childOfChildofChildNode.InnerText.Trim().Contains("Required"))
                                                {
                                                    // MessageBox.Show(childOfChildofChildNode.InnerText.Trim(), "12");
                                                    string str = childOfChildofChildNode.InnerText.Replace("Required on", "");//Required Date

                                                    XmlElement RequiredDateElement = xmlDoc.CreateElement("RequiredDate");
                                                    RequiredDateElement.InnerText = str;
                                                    rootElement.AppendChild(RequiredDateElement);
                                                }

                                                if (value == "PROPERTY")
                                                {
                                                    //    MessageBox.Show(childOfChildofChildNode.InnerText, "2");//Property Comments

                                                    XmlElement PropertyDetailsElement = xmlDoc.CreateElement("PropertyDetails");
                                                    PropertyDetailsElement.InnerText = childOfChildofChildNode.InnerText;
                                                    rootElement.AppendChild(PropertyDetailsElement);

                                                    HtmlNodeCollection propertycomments = htmlDoc.DocumentNode.SelectNodes(".//i");
                                                    string str = propertycomments[1].InnerText;//Property Comments
                                                    value = "";

                                                    XmlElement PropertyCommentsElement = xmlDoc.CreateElement("PropertyComments");
                                                    PropertyCommentsElement.InnerText = str;
                                                    rootElement.AppendChild(PropertyCommentsElement);

                                                }
                                                if (childOfChildofChildNode.InnerText.Trim() == "PROPERTY")
                                                {
                                                    value = "PROPERTY";
                                                    continue;
                                                }

                                            }
                                        }

                                    }
                                }
                            }
                        }
                    }

                    HtmlNodeCollection Nds = htmlDoc.DocumentNode.SelectNodes(".//table[@class='MsoNormalTable']");
                    int cnt = 1;
                    foreach (HtmlNode childNds in Nds)
                    {

                        if (childNds.ChildNodes.Count > 0)
                        {
                            if (childNds.InnerText.Contains("This is an order"))
                            {
                                continue;

                            }
                            else
                            {
                                if (childNds.ChildNodes.Count > 0)
                                {

                                    foreach (HtmlNode childofChildNds in childNds.ChildNodes)
                                    {
                                        XmlElement OrderItemDetailsElement = xmlDoc.CreateElement("OrderItemDetails");
                                        if (childofChildNds.InnerText.Trim() != "" && childofChildNds.InnerText.Trim() != "Client PriceCost Price" && childofChildNds.InnerText.Trim() != "campaigntrack.com.auhelpdesk")
                                        {
                                            string str = childofChildNds.InnerText;
                                            str = str.Replace("$", "|");

                                            String[] SplitValue = Splits(str, new char[] { '|' });
                                            string str1 = SplitValue[0].ToString().Trim();
                                            string str2 = SplitValue[1].ToString().Trim();
                                            string str3 = SplitValue[2].ToString().Trim();
                                            //MessageBox.Show(str1 + ' ' + str2 + ' ' + str3);

                                            //for (int i = 1; i < SplitValue.Length; i++)
                                            //{
                                            //    if (i != 4 && i != 10 && i != 16) { continue; }
                                            //    if (str1 == "   " || str2 == "   " || str3 == "   ") { continue; }
                                            //    string str = SplitValue[i];
                                            //    XmlElement IdElement = xmlDoc.CreateElement("n");
                                            //    //cnt = cnt + 1;
                                            //    IdElement.InnerText = cnt.ToString() + "_" + str1 + "|" + str2 + "|" + str3;
                                            //    OrderItemDetailsElement.AppendChild(IdElement);
                                            //}
                                            if (str1 != "" || str2 != "" || str3 != "")
                                            {
                                                XmlElement IdElement = xmlDoc.CreateElement("n");
                                                IdElement.InnerText = cnt.ToString() + "_" + str1 + "|" + str2 + "|" + str3;
                                                OrderItemDetailsElement.AppendChild(IdElement);
                                                rootElement.AppendChild(OrderItemDetailsElement);
                                            }
                                        }

                                    }
                                    { cnt = cnt + 1; }
                                }

                            }

                        }
                    }
                    string strd = xmlDoc.InnerXml.ToString();
                    strd = strd.Replace("&amp;", "and").Replace("andamp;", " and ");
                    ExecuteProcess(strd);
                    xmlDoc.RemoveAll();
                    System.IO.File.Move(System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\" + filename, System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\Archive\\" + filename);
                    filename = filename.Replace("htm", "txt");
                    System.IO.File.Move(System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\" + filename, System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\Archive\\" + filename);

                    // s.A1CreateEvent("*****@*****.**", "abc", "03-12-2013", "04-12-2013", "Test", "def", "ghi");
                }

                DataSet dsRecordtoProcess = new DataSet();
                DataTable dtRecordtoProcess = new DataTable();
                dsRecordtoProcess = ReturnDataforCalenderEntry();
                dtRecordtoProcess = dsRecordtoProcess.Tables[0];
                GoogleCalSvc s = new GoogleCalSvc();
                if (dtRecordtoProcess.Rows.Count  > 0)
                {
                    for (int i = 0; i < dtRecordtoProcess.Rows.Count; i++)
                    {
                        s.userName = dtRecordtoProcess.Rows[i]["UserName"].ToString();
                        s.userPassword = dtRecordtoProcess.Rows[i]["Password"].ToString();
                        s.CreateEvent(dtRecordtoProcess.Rows[i]["Name"].ToString(), dtRecordtoProcess.Rows[i]["Startdate"].ToString(), dtRecordtoProcess.Rows[i]["EndDate"].ToString());
                        //  s.A1CreateEvent("*****@*****.**", "abc", "05-12-2013", "06-12-2013", "Test", "def", "ghi");
                    }
                }
            }
            catch (Exception ex)
            {
                strLog = ex.Message;
                WriteLog(strLog);
            }

            return strLog;
        }
Ejemplo n.º 3
0
        public string ProcessData()
        {
            string strLog = "";
            XmlDocument xmlDoc = new XmlDocument();
            IOAuthSession session;
            DateTime DueDate = DateTime.Now;
            string strd;
            string Subject;

            try
            {
                session = XeroAPI();
                Repository repository = new Repository(session);
                OpreateData opftp = new OpreateData();
                string strLogs = opftp.GetDataFromServer(ref connectionSuccess);//, ref index);
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                DataSet dsProductNotification = new DataSet();
                Email.Email eml = new Email.Email();
                String[] OrderId = null;
                foreach (string file in Directory.EnumerateFiles(FullPath, "*.htm"))
                {
                    filename = Path.GetFileName(file);
                    // filename = "201404161759591372889.htm";
                    htmlDoc.Load(file);
                    StartBrowser(htmlDoc.DocumentNode.OuterHtml);

                }

                // }
                foreach (string file in Directory.EnumerateFiles(FullPath, "*.htm"))
                {

                    filename = Path.GetFileName(file);
                    // filename = "201404161759591372889.htm";
                    XmlNode rootElement = xmlDoc.CreateElement("Root");
                    xmlDoc.AppendChild(rootElement);
                    htmlDoc.Load(file);
                    htmlDoc.OptionWriteEmptyNodes = true;
                    htmlDoc.OptionOutputAsXml = true;
                    string value = "";
                    HtmlNodeCollection nodesitemheading = htmlDoc.DocumentNode.SelectNodes(".//b");
                    /*This is an order notification from Robin Parker at Marshall White Brighton<br />
                    <i>Relating to
                     campaign: 11 Lagnicourt Street, Hampton</i> */

                    String[] SplitValues = new String[1000];

                    XmlElement Element = xmlDoc.CreateElement("PrimaryContact");
                    try
                    {
                        SplitValues = Splits(sep(nodesitemheading[0].InnerText, "from "), new char[] { ' ' });
                    }
                    catch
                    {
                        File.Move(file, System.Configuration.ConfigurationSettings.AppSettings["Error"] + "\\" + filename);
                        File.Move(file.Replace("htm", "txt"), System.Configuration.ConfigurationSettings.AppSettings["Error"] + "\\" + filename.Replace("htm", "txt"));
                        xmlDoc.RemoveAll();
                        continue;
                    };

                    string cncl1 = "", cncl2 = "", cncl3 = "";

                    if (nodesitemheading[0].InnerText.Contains("The order listed below was cancelled by"))
                    {

                        IList<string> arrayAsList = (IList<string>)SplitValues;
                        int index;
                        index = arrayAsList.IndexOf("campaign:");
                        if (index == -1)
                            index = arrayAsList.IndexOf("\r\ncampaign:");
                        for (int i = index + 1; i < SplitValues.Length; i++)
                        {
                            if (SplitValues[i].Contains("of")) { break; };
                            cncl1 = cncl1 + " " + SplitValues[i];//email body subject
                            cncl1 = cncl1.Replace("Description", "").Replace("\r\n", "");
                        }

                        IList<string> arrayAsList1 = (IList<string>)SplitValues;
                        int index1 = arrayAsList.IndexOf("being");
                        for (int i = index1 + 2; i < SplitValues.Length; i++)
                        {
                            if (SplitValues[i].Contains("on")) { break; };
                            cncl2 = cncl2 + " " + SplitValues[i];//email body subject
                            cncl2 = cncl2.Replace("Required", "").Replace("\r\n", "");
                        }
                        IList<string> arrayAsList2 = (IList<string>)SplitValues;
                        int index2 = arrayAsList.IndexOf("on");
                        cncl3 = SplitValues[index2 + 1].Replace("Product", "");

                        InsertCancelledOrders(cncl1, cncl2, cncl3, filename);
                        File.Move(file, System.Configuration.ConfigurationSettings.AppSettings["Cancelled"] + "\\" + filename);
                        File.Move(file.Replace("htm", "txt"), System.Configuration.ConfigurationSettings.AppSettings["Cancelled"] + "\\" + filename.Replace("htm", "txt"));
                        xmlDoc.RemoveAll();
                        continue;
                    }

                    string str11 = "", str22 = "", str33 = "";
                    try
                    {

                        str11 = SplitValues[0] + " " + SplitValues[1];//primary contact
                        str22 = SplitValues[3] + " " + SplitValues[4] + " " + SplitValues[5];//company name
                        str22 = str22.Replace("Relating", "");

                        //if (SplitValues[7].Contains("to"))
                        //{
                        //    str33 = "";
                        //    for (int i = 8; i < SplitValues.Length; i++)
                        //    {
                        //        str33 = str33 + " " + SplitValues[i];//email body subject
                        //    }
                        //}
                        //else
                        //    str33 = SplitValues[7] + " " + SplitValues[8] + " " + SplitValues[9] + " " + SplitValues[10] + SplitValues[11];//email body subject

                        IList<string> arrayAsList = (IList<string>)SplitValues;
                        int index;
                        index = arrayAsList.IndexOf("Relating".Replace("\r\n", ""));
                        if (index == -1)
                            index = arrayAsList.IndexOf("\r\ncampaign:");//campaign:
                        if (index == -1)
                            index = arrayAsList.IndexOf("campaign:");//campaign:

                        for (int i = index + 1; i < SplitValues.Length; i++)
                        {
                            if (SplitValues[i].Contains("of")) { break; };
                            str33 = str33 + " " + SplitValues[i];//email body subject
                            str33 = str33.Replace("Description", "").Replace("\r\n", "");
                        }

                        //if (SplitValues[7].Contains("Relating"))
                        //{
                        //    str33 = "";
                        //    for (int i = 10; i < SplitValues.Length; i++)
                        //    {
                        //        str33 = str33 + " " + SplitValues[i];//email body subject
                        //    }
                        //}
                    }
                    catch
                    {
                        File.Move(file, System.Configuration.ConfigurationSettings.AppSettings["Error"] + "\\" + filename);
                        File.Move(file.Replace("htm", "txt"), System.Configuration.ConfigurationSettings.AppSettings["Error"] + "\\" + filename.Replace("htm", "txt"));
                        xmlDoc.RemoveAll();
                        continue;
                    };
                    XmlElement PrimaryContactElement = xmlDoc.CreateElement("PrimaryContact");
                    PrimaryContactElement.InnerText = str11;
                    rootElement.AppendChild(PrimaryContactElement);

                    XmlElement CompanyNameElement = xmlDoc.CreateElement("CompanyName");
                    CompanyNameElement.InnerText = str22;
                    rootElement.AppendChild(CompanyNameElement);

                    XmlElement EmailBodySubjectElement = xmlDoc.CreateElement("EmailBodySubject");
                    EmailBodySubjectElement.InnerText = nodesitemheading[0].InnerText;
                    rootElement.AppendChild(EmailBodySubjectElement);

                    XmlElement PropertyNameElement = xmlDoc.CreateElement("PropertyName");
                    str33 = str33.Replace("campaign:", "");
                    PropertyNameElement.InnerText = str33;
                    rootElement.AppendChild(PropertyNameElement);

                    XmlElement FileNameElement = xmlDoc.CreateElement("FileName");
                    FileNameElement.InnerText = Path.GetFileName(file);
                    rootElement.AppendChild(FileNameElement);
                    string strfile = file.Replace("htm", "txt");
                    string[] lines = File.ReadAllLines(strfile);

                    string From = lines[0].ToString().Replace("From:", "");
                    XmlElement FromElement = xmlDoc.CreateElement("From");
                    FromElement.InnerText = From;
                    rootElement.AppendChild(FromElement);

                    string SentDate = lines[1].ToString().Replace("SentDate:", "");
                    XmlElement SentDateElement = xmlDoc.CreateElement("SentDate");
                    //DateTime objSentDate = Convert.ToDateTime(SentDate,CultureInfo.CreateSpecificCulture("en-US"));
                    DateTime objSentDate = DateTime.Parse(SentDate, CultureInfo.GetCultureInfo("en-gb"));
                   // SentDateElement.InnerText = objSentDate.Month.ToString().PadLeft(2, '0') + "/" + objSentDate.Day.ToString().PadLeft(2, '0') + "/" + objSentDate.Year + " " + objSentDate.ToLongTimeString();
                    rootElement.AppendChild(SentDateElement);

                    Subject = lines[2].ToString().Replace("Subject:", "");
                    XmlElement SubjectElement = xmlDoc.CreateElement("Subject");
                    SubjectElement.InnerText = Subject;
                    rootElement.AppendChild(SubjectElement);

                    HtmlNodeCollection nodesitemcontacts = htmlDoc.DocumentNode.SelectNodes(".//td[@class='content']");
                    //htmlDoc.DocumentNode.SelectNodes(".//td[@class='content']")[0].ChildNodes
                    foreach (HtmlNode childNode in nodesitemcontacts)
                    {
                        if (childNode.ChildNodes.Count > 0)
                        {
                            foreach (HtmlNode childOfChildNode in childNode.ChildNodes)
                            {
                                if (childOfChildNode.Name == "#text")
                                {
                                    if (childOfChildNode.ChildNodes.Count == 0)
                                    {

                                        if (childOfChildNode.InnerText.Trim().Contains("Property ID") || childOfChildNode.InnerText.Trim().Contains("Property"))
                                        {
                                            //MessageBox.Show(childOfChildofChildNode.InnerText.Trim().Replace("Property ID:", ""));//Property Id
                                            XmlElement PropertyIdElement = xmlDoc.CreateElement("PropertyId");

                                            PropertyIdElement.InnerText = childOfChildNode.InnerText.Trim().Replace("Property ID:", "").Replace("\r\n", "").Replace("Property ID:", "");
                                            rootElement.AppendChild(PropertyIdElement);

                                        }

                                        if (childOfChildNode.InnerText.Trim().Contains("OrderItem"))
                                        {
                                            // MessageBox.Show(childOfChildofChildNode.InnerText.Trim().Replace("OrderItem:", ""));
                                            String[] SplitValue = Splits(childOfChildNode.InnerText.Trim().Replace("OrderItem:", ""), new char[] { ',' });//Order Item Id
                                            OrderId = SplitValue;
                                            SplitValueOrderItemId = Splits(childOfChildNode.InnerText.Trim().Replace("OrderItem:", ""), new char[] { ',' });
                                            XmlElement OrderItemIdElement = xmlDoc.CreateElement("OrderItemId");
                                            for (int i = 0; i < SplitValue.Length; i++)
                                            {
                                                string str = SplitValue[i];
                                                XmlElement IdElement = xmlDoc.CreateElement("Id");
                                                IdElement.InnerText = str.Trim();
                                                OrderItemIdElement.AppendChild(IdElement);
                                            }
                                            rootElement.AppendChild(OrderItemIdElement);
                                        }

                                        if (childOfChildNode.InnerText.Trim().Contains("Sales contacts"))
                                        {
                                            // MessageBox.Show(childOfChildofChildNode.InnerText.Trim().Replace("Sales contacts:", ""), "8");
                                            String[] SplitValue = Splits(childOfChildNode.InnerText.Trim().Replace("Sales contacts:", ""), new char[] { ',' });
                                            for (int i = 0; i < SplitValue.Length; i++)
                                            {
                                                string str = (SplitValue[i].Replace(" on ", ",").Replace("\r\non", ","));
                                                String[] strValue = Splits(str, new char[] { ',' });

                                                string str1 = strValue[0];//Sales contact name
                                                string str2 = strValue[1].Replace("or", "");//Sales contact phone number

                                                XmlElement SalesContactElement = xmlDoc.CreateElement("SalesContact");
                                                SalesContactElement.InnerText = str1.Trim(); ;
                                                rootElement.AppendChild(SalesContactElement);

                                                XmlElement SalesContactNumberElement = xmlDoc.CreateElement("SalesContactNumber");
                                                SalesContactNumberElement.InnerText = str2.Trim();
                                                rootElement.AppendChild(SalesContactNumberElement);

                                            }

                                            HtmlNodeCollection adminemail = htmlDoc.DocumentNode.SelectNodes(".//a[@href]");
                                            string stradmin = adminemail[1].InnerText;//Admin email address

                                        }

                                        if (childOfChildNode.InnerText.Trim().Contains("Admin contact"))
                                        {
                                            // MessageBox.Show(childOfChildofChildNode.InnerText.Trim(), "9");
                                            // String[] SplitValue = Splits(childOfChildNode.InnerText.Trim().Replace("Admin contact:", "").Replace(" on", ","), new char[] { ',' });
                                            String[] SplitValue = Splits(childOfChildNode.InnerText.Trim().Replace("Admin contact:", "").Replace(" on", ","), "or".ToCharArray());

                                            string admindetails = childOfChildNode.InnerText.Trim().Replace("Admin contact:", "");//Admin contact name
                                            string str1;
                                            try
                                            {
                                                // bool string1Matched = Regex.IsMatch(string1, @"\bthe\b", RegexOptions.IgnoreCase);
                                                str1 = admindetails.Substring(0, admindetails.IndexOf(" or "));
                                                str1 = str1.Substring(0, str1.IndexOf(" on "));//Admin name
                                                string str2;
                                                str2 = admindetails.Substring(0, admindetails.IndexOf(" or "));
                                                str2 = str2.Substring(str2.LastIndexOf(" on ") + 3);//Admin Phone

                                                string str3 = "";
                                                str3 = admindetails.Substring(admindetails.IndexOf(" or ") + 4);
                                                XmlElement AdminContactElement = xmlDoc.CreateElement("AdminContact");
                                                AdminContactElement.InnerText = str1.Trim();
                                                rootElement.AppendChild(AdminContactElement);

                                                XmlElement AdminContactNumberElement = xmlDoc.CreateElement("AdminContactNumber");
                                                AdminContactNumberElement.InnerText = str2.Trim();
                                                rootElement.AppendChild(AdminContactNumberElement);

                                                XmlElement AdminContactEmailElement = xmlDoc.CreateElement("AdminContactEmail");
                                                AdminContactEmailElement.InnerText = str3.Trim();
                                                rootElement.AppendChild(AdminContactEmailElement);
                                            }
                                            catch (Exception ex)
                                            {
                                                strLog = ex.StackTrace.ToString();
                                                WriteLog(strLog + " " + filename);
                                            }

                                        }

                                        if (childOfChildNode.InnerText.Trim().Contains("Supplier:"))
                                        {
                                            //MessageBox.Show(childOfChildofChildNode.InnerText.Trim(), "10");
                                            string str1 = childOfChildNode.InnerText.Replace("Supplier:", "");//Product

                                            XmlElement SupplierElement = xmlDoc.CreateElement("Supplier");
                                            SupplierElement.InnerText = str1.Trim();
                                            rootElement.AppendChild(SupplierElement);
                                        }

                                        if (childOfChildNode.InnerText.Trim().Contains("Description of product/service being ordered:"))
                                        {
                                            value = "Description of product";
                                            continue;
                                        }

                                        if (value == "Description of product")
                                        {
                                            //MessageBox.Show(childOfChildofChildNode.InnerText.Trim(), "11");
                                            string str = childOfChildNode.InnerText.Replace("Description of product/service being ordered: ", "");//Product Description
                                            value = "";

                                            XmlElement DescriptionElement = xmlDoc.CreateElement("Description");
                                            DescriptionElement.InnerText = str.Trim();
                                            rootElement.AppendChild(DescriptionElement);
                                        }

                                        if (childOfChildNode.InnerText.Trim().Contains("Required"))
                                        {
                                            // MessageBox.Show(childOfChildofChildNode.InnerText.Trim(), "12");

                                            string RequiredDate = childOfChildNode.InnerText.Replace("Required on", "").Replace("Required \r\non", "").Replace("\r\n", "");//Required Date
                                            DateTime objRequiredDate = DateTime.Parse(RequiredDate, CultureInfo.GetCultureInfo("en-gb"));
                                            XmlElement RequiredDateElement = xmlDoc.CreateElement("RequiredDate");
                                            //RequiredDateElement.InnerText = objRequiredDate.Mo.ToString().PadLeft(2, '0') + "/" + objRequiredDate.Month.ToString().PadLeft(2, '0') + "/" + objRequiredDate.Year;
                                            RequiredDateElement.InnerText = objRequiredDate.ToString();
                                            rootElement.AppendChild(RequiredDateElement);
                                            DueDate = Convert.ToDateTime(RequiredDateElement.InnerText);
                                        }

                                        if (value == "PROPERTY")
                                        {
                                            //    MessageBox.Show(childOfChildofChildNode.InnerText, "2");//Property Comments

                                            XmlElement PropertyDetailsElement = xmlDoc.CreateElement("PropertyDetails");
                                            PropertyDetailsElement.InnerText = childOfChildNode.InnerText;
                                            rootElement.AppendChild(PropertyDetailsElement);

                                            HtmlNodeCollection propertycomments = htmlDoc.DocumentNode.SelectNodes(".//i");
                                            string str = propertycomments[1].InnerText;//Property Comments
                                            value = "";

                                            XmlElement PropertyCommentsElement = xmlDoc.CreateElement("PropertyComments");
                                            PropertyCommentsElement.InnerText = str.Trim();
                                            rootElement.AppendChild(PropertyCommentsElement);

                                        }
                                        if (childOfChildNode.InnerText.Trim() == "PROPERTY")
                                        {
                                            value = "PROPERTY";
                                            continue;
                                        }

                                        //}
                                        // }

                                    }
                                }
                            }
                        }
                    }

                    HtmlNodeCollection Nds = htmlDoc.DocumentNode.SelectNodes(".//table[@cellpadding='3']");
                    int cnt = 1;

                    if (Nds == null)
                    {
                        if (Subject.Contains("cancellation"))
                        {
                            File.Move(file, System.Configuration.ConfigurationSettings.AppSettings["Cancelled"] + "\\" + filename);
                            File.Move(file.Replace("htm", "txt"), System.Configuration.ConfigurationSettings.AppSettings["Cancelled"] + "\\" + filename.Replace("htm", "txt"));

                        }
                    };

                    if (Nds != null)
                    {
                        foreach (HtmlNode childNds in Nds)
                        {

                            if (childNds.ChildNodes.Count > 0)
                            {
                                if (childNds.InnerText.Contains("This is an order"))
                                {
                                    continue;

                                }
                                else
                                {
                                    if (childNds.ChildNodes.Count > 0)
                                    {

                                        foreach (HtmlNode childofChildNds in childNds.ChildNodes)
                                        {
                                            XmlElement OrderItemDetailsElement = xmlDoc.CreateElement("OrderItemDetails");
                                            if (childofChildNds.InnerText.Trim() != "" && childofChildNds.InnerText.Contains("Client PriceCost") == false && childofChildNds.InnerText.Contains("Client \r\nPriceCost") == false && childofChildNds.InnerText.Trim() != "campaigntrack.com.auhelpdesk")
                                            {
                                                string str = childofChildNds.InnerText;
                                                str = str.Replace("$", "|");

                                                String[] SplitValue = Splits(str, new char[] { '|' });
                                                string str1 = SplitValue[0].ToString().Trim();
                                                string str2 = SplitValue[1].ToString().Trim();
                                                string str3 = SplitValue[2].ToString().Trim();
                                                //MessageBox.Show(str1 + ' ' + str2 + ' ' + str3);

                                                //for (int i = 1; i < SplitValue.Length; i++)
                                                //{
                                                //    if (i != 4 && i != 10 && i != 16) { continue; }
                                                //    if (str1 == "   " || str2 == "   " || str3 == "   ") { continue; }
                                                //    string str = SplitValue[i];
                                                //    XmlElement IdElement = xmlDoc.CreateElement("n");
                                                //    //cnt = cnt + 1;
                                                //    IdElement.InnerText = cnt.ToString() + "_" + str1 + "|" + str2 + "|" + str3;
                                                //    OrderItemDetailsElement.AppendChild(IdElement);
                                                //}
                                                if (str1 != "" || str2 != "" || str3 != "")
                                                {
                                                    XmlElement IdElement = xmlDoc.CreateElement("n");
                                                    IdElement.InnerText = cnt.ToString() + "_" + str1 + "|" + str2 + "|" + str3;
                                                    OrderItemDetailsElement.AppendChild(IdElement);
                                                    rootElement.AppendChild(OrderItemDetailsElement);
                                                }

                                                //dsProductNotification = ProductEmailNotification(str1);
                                                //if (dsProductNotification.Tables[0].Rows.Count > 0)
                                                //{

                                                //    //(String subject,String Message,string To,string CC, bool hasattachment)
                                                //    //   eml.SendMail("", "", dsProductNotification.Tables[0].Rows[0]["EmailTo"].ToString(), dsProductNotification.Tables[0].Rows[0]["EmailCC"].ToString(), false);

                                                //}
                                            }

                                        }
                                        { cnt = cnt + 1; }
                                    }

                                }

                            }
                        }
                    }
                    strd = xmlDoc.InnerXml.ToString();
                    strd = strd.Replace("&amp;", "and").Replace("andamp;", " and ");
                    strd = strd.Replace("'", "''");
                    DataSet ds = new DataSet();
                    ds = ExecuteProcess(strd,htmlDoc.DocumentNode.OuterHtml);
                    xmlDoc.RemoveAll();

                    for (int j = 0; j < ds.Tables[1].Rows.Count; j++)
                    {
                        dsProductNotification = ProductSchedule((int)ds.Tables[1].Rows[j]["Xero_Id"]);

                        if (dsProductNotification.Tables[0].Rows.Count > 0)
                        {

                            //(String subject,String Message,string To,string CC, bool hasattachment)
                            //   eml.SendMail("", "", dsProductNotification.Tables[0].Rows[0]["EmailTo"].ToString(), dsProductNotification.Tables[0].Rows[0]["EmailCC"].ToString(), false);

                            if (dsProductNotification.Tables[0].Rows[dsProductNotification.Tables[0].Rows.Count - 1]["Value"].ToString().Contains("@"))
                            {
                                // eml.SendMail(Subject, htmlDoc.DocumentNode.OuterHtml, dsProductNotification.Tables[0].Rows[0]["EmailTo"].ToString(), "", false);
                                eml.SendMail(Subject, htmlDoc.DocumentNode.OuterHtml, "*****@*****.**", "", false);

                            }
                            else
                            {
                                DataSet dsRecordtoProcess = new DataSet();
                                DataTable dtRecordtoProcess = new DataTable();
                                dsRecordtoProcess = ReturnDataforCalenderEntry(ds.Tables[1].Rows[j]["OrderId"].ToString());
                                dtRecordtoProcess = dsRecordtoProcess.Tables[0];
                                GoogleCalSvc s = new GoogleCalSvc();
                                if (dtRecordtoProcess.Rows.Count > 0)
                                {
                                    for (int i = 0; i < dtRecordtoProcess.Rows.Count; i++)
                                    {

                                        s.CreateEvent(dtRecordtoProcess.Rows[i]["Title"].ToString(), dtRecordtoProcess.Rows[i]["Location"].ToString(), dtRecordtoProcess.Rows[i]["SalesContact"].ToString(), dtRecordtoProcess.Rows[i]["Startdate"].ToString(), dtRecordtoProcess.Rows[i]["Enddate"].ToString(), dtRecordtoProcess.Rows[i]["Comments"].ToString(), dtRecordtoProcess.Rows[i]["ProductDescription"].ToString(), dtRecordtoProcess.Rows[i]["OrderId"].ToString(), dtRecordtoProcess.Rows[i]["RequiredDate"].ToString(), dtRecordtoProcess.Rows[i]["ColorId"].ToString());
                                        //UpdateOrderEvent((int)ds.Tables[1].Rows[j]["row_id1"]);//OrderId
                                        //  s.A1CreateEvent("*****@*****.**", "abc", "05-12-2013", "06-12-2013", "Test", "def", "ghi");
                                    }
                                }
                            }
                        }
                        UpdateOrderEvent((int)ds.Tables[1].Rows[j]["row_id1"]);//OrderId
                    }

                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                    {
                        if (ds.Tables[0].Rows[j]["CreateInvoice"].ToString() == "True")
                        {
                            Invoice invoice = new Invoice();
                            invoice.Type = "ACCREC";
                            invoice.Contact = new Contact { Name = ds.Tables[0].Rows[j]["XeroName"].ToString() };
                            invoice.Date = DateTime.Today;
                            invoice.DueDate = DueDate;
                            invoice.Status = "DRAFT";
                            invoice.TotalTax = 0;
                            invoice.HasAttachments = true;
                            invoice.LineAmountTypes = XeroApi.Model.LineAmountType.Inclusive;
                            invoice.Reference = ds.Tables[0].Rows[j]["Property"].ToString();
                            LineItems lineitems = new LineItems();
                            invoice.LineItems = new XeroApi.Model.LineItems();
                            ds.Tables[1].DefaultView.RowFilter = "OrderId='" + ds.Tables[0].Rows[j]["OrderId"].ToString() + "'";
                            for (int i = 0; i < ds.Tables[1].DefaultView.Count; i++)
                            {

                                LineItem lineItem = new LineItem();
                                lineItem.ItemCode = ds.Tables[1].DefaultView[i]["XeroCode"].ToString();
                                lineItem.Description = ds.Tables[1].DefaultView[i]["XeroItemDescription"].ToString();
                                lineItem.Quantity = 1;
                                lineItem.UnitAmount = Convert.ToDecimal(ds.Tables[1].DefaultView[i]["Cost1"]);
                                lineItem.TaxType = ds.Tables[1].DefaultView[i]["SalesTaxType"].ToString().Replace("GST on Income", "OUTPUT");
                                lineItem.AccountCode = ds.Tables[1].DefaultView[i]["SalesAccountCode"].ToString();
                                invoice.LineItems.Add(lineItem);
                            }

                            var inv = repository.Create<XeroApi.Model.Invoice>(invoice);
                            if (invoice.ValidationStatus == ValidationStatus.ERROR)
                            {
                                foreach (var message in invoice.ValidationErrors)
                                {
                                }
                            }
                            try
                            {
                                Guid invoiceId = inv.InvoiceID;
                                string invoiceNo = inv.InvoiceNumber;
                                // System.Threading.Thread.Sleep(5000);
                                string AnyAttachmentFilename = FullPath + "\\" + filename.Replace("htm", "jpg");
                                var SalesInvoice = repository.Invoices.FirstOrDefault(it => it.InvoiceID == invoiceId);
                                var newAttachment = repository.Attachments.Create(SalesInvoice, new FileInfo(AnyAttachmentFilename));
                                InsertProductInvoiceNo((int)ds.Tables[1].Rows[j]["Row_Id"], ds.Tables[0].Rows[j]["OrderId"].ToString(), invoiceNo);
                                ds.Tables[1].DefaultView.RowFilter = string.Empty;

                            }
                            catch (Exception ex)
                            {
                                strLog = ex.StackTrace.ToString();
                                WriteLog(strLog + " " + filename);
                                File.Move(System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\" + filename, System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\Error\\" + filename);
                            };
                        }

                    }
                    System.Threading.Thread.Sleep(30000);
                    System.IO.File.Delete(System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\" + filename);
                    filename = filename.Replace("htm", "txt");
                    System.IO.File.Delete(System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\" + filename);
                    //filename = filename.Replace("txt", "jpg");
                    //System.IO.File.Move(System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\" + filename, System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\Archive\\" + filename);
                }

            }
            catch (Exception ex)
            {
                strLog = ex.StackTrace.ToString();
                WriteLog(strLog + " " + filename);
                File.Move(System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\" + filename, System.Configuration.ConfigurationSettings.AppSettings["MailPath"] + "\\Error\\" + filename);
            }

            return strLog;
        }