protected void ReadSentEmails()
    {
        try
        {
            #region Email server connetion
            var mailRepository = new MailRepository(
                                       ConfigurationManager.ConnectionStrings["ServerName"].ConnectionString,
                                       int.Parse(ConfigurationManager.ConnectionStrings["PortNo"].ConnectionString),
                                       true,
                                       strUsrName,
                                       strPwd
                                   );
            var emailList = mailRepository.GetAllMails("Sent");
            #endregion

            #region Process Email Fields
            foreach (Message email in emailList)
            {
                strMsgId += email.MessageId + "|";
                foreach (Address emailid in email.To)
                {
                    if (strEmailids == "")
                        strEmailids += emailid;
                    if (strWebsite == "")
                        strWebsite = Convert.ToString(emailid).Substring((Convert.ToString(emailid)).IndexOf("@") + 1);
                }

                companyWebsite += "www." + strWebsite + "|";
                strWebsite = "";

                //foreach (Address emailid in email.Cc)  {  }
                //foreach (Address emailid in email.Bcc) {  }

                clientEmailId += strEmailids + "|"; //Contains all To, CC, BCC email ids
                strEmailids = "";

                sentDate += email.Date + "|";

                fromEmailId += Convert.ToString(email.From) + "|";

                //strEmail += "<br><b>ReceivedDate :</b> :" + email.ReceivedDate;
                //strEmail += "<br><b>MessageId</b> :" + email.MessageId;
                //strEmail += "<br><b>Subject</b> :" + email.Subject;
                strEmailBody = Convert.ToString(email.BodyText.Text);
                if (strEmailBody.Contains("Good Morning"))
                {
                    int i = strEmailBody.IndexOf("Good Morning");
                    string strTempEBody = strEmailBody.Substring(i);
                    int j = strTempEBody.IndexOf(",");
                    int k = ("Good Morning ").Length;
                    strEmail += "<br><b>Client Name</b> :" + strEmailBody.Substring((i + k), (j - k));

                    clientName += strEmailBody.Substring((i + k), (j - k)) + "|";
                }
                else
                    clientName += "|";

                if (strEmailBody.Contains("partnering with companies such as"))
                {
                    int i = strEmailBody.IndexOf("partnering with companies such as");
                    string strTempEBody = strEmailBody.Substring(i);
                    int j = strTempEBody.IndexOf(",");
                    int k = ("partnering with companies such as ").Length;
                    strEmail += "<br><b>Company Name</b> :" + strEmailBody.Substring((i + k), (j - k));

                    companyName += strEmailBody.Substring((i + k), (j - k)) + "|";
                }
                else
                    companyName += "|";

                //if (email.Attachments.Count > 0)
                //{
                //    foreach (MimePart attachment in email.Attachments)
                //    {
                //        strEmail += "<p>Attachment:" + attachment.ContentName + "  " + attachment.ContentType.MimeType + "</p><br>";
                //    }
                //}
            }
            #endregion

            #region Insert Records into Database
            insertRecords(sentDate, companyWebsite, companyName, clientEmailId, clientName, fromEmailId);
            #endregion

            #region Move processed emails into other folder
            mailRepository.MoveTOProcessedFolder("Sent", " Sent.ProcessedMails", strMsgId);
            #endregion

            divMsg.InnerHtml = "<b> <font color=\"red\"> All Emails are scanned successfully. </font> </b>";
        }
        catch (Exception ex)
        {
            btnScanEmails.Visible = false;
            btnExportToExcel.Visible = false;
            if (ex.ToString().Contains("failed"))
                divError.InnerHtml = "<b> <font color=\"red\"> LOGIN FAILED......... </font> </b>";  // +ex.ToString();
            else
                divError.InnerText = Convert.ToString(ex);
        }
    }
    protected void ReadSentEmails()
    {
        try
        {
            #region Email server connetion
            var mailRepository = new MailRepository(
                ConfigurationManager.ConnectionStrings["ServerName"].ConnectionString,
                int.Parse(ConfigurationManager.ConnectionStrings["PortNo"].ConnectionString),
                true,
                strUsrName,
                strPwd
                );
            var emailList = mailRepository.GetAllMails("Sent");
            #endregion

            #region Process Email Fields
            foreach (Message email in emailList)
            {
                strMsgId += email.MessageId + "|";
                foreach (Address emailid in email.To)
                {
                    if (strEmailids == "")
                    {
                        strEmailids += emailid;
                    }
                    if (strWebsite == "")
                    {
                        strWebsite = Convert.ToString(emailid).Substring((Convert.ToString(emailid)).IndexOf("@") + 1);
                    }
                }

                companyWebsite += "www." + strWebsite + "|";
                strWebsite      = "";

                //foreach (Address emailid in email.Cc)  {  }
                //foreach (Address emailid in email.Bcc) {  }

                clientEmailId += strEmailids + "|"; //Contains all To, CC, BCC email ids
                strEmailids    = "";

                sentDate += email.Date + "|";

                fromEmailId += Convert.ToString(email.From) + "|";

                //strEmail += "<br><b>ReceivedDate :</b> :" + email.ReceivedDate;
                //strEmail += "<br><b>MessageId</b> :" + email.MessageId;
                //strEmail += "<br><b>Subject</b> :" + email.Subject;
                strEmailBody = Convert.ToString(email.BodyText.Text);
                if (strEmailBody.Contains("Good Morning"))
                {
                    int    i            = strEmailBody.IndexOf("Good Morning");
                    string strTempEBody = strEmailBody.Substring(i);
                    int    j            = strTempEBody.IndexOf(",");
                    int    k            = ("Good Morning ").Length;
                    strEmail += "<br><b>Client Name</b> :" + strEmailBody.Substring((i + k), (j - k));

                    clientName += strEmailBody.Substring((i + k), (j - k)) + "|";
                }
                else
                {
                    clientName += "|";
                }

                if (strEmailBody.Contains("partnering with companies such as"))
                {
                    int    i            = strEmailBody.IndexOf("partnering with companies such as");
                    string strTempEBody = strEmailBody.Substring(i);
                    int    j            = strTempEBody.IndexOf(",");
                    int    k            = ("partnering with companies such as ").Length;
                    strEmail += "<br><b>Company Name</b> :" + strEmailBody.Substring((i + k), (j - k));

                    companyName += strEmailBody.Substring((i + k), (j - k)) + "|";
                }
                else
                {
                    companyName += "|";
                }

                //if (email.Attachments.Count > 0)
                //{
                //    foreach (MimePart attachment in email.Attachments)
                //    {
                //        strEmail += "<p>Attachment:" + attachment.ContentName + "  " + attachment.ContentType.MimeType + "</p><br>";
                //    }
                //}
            }
            #endregion

            #region Insert Records into Database
            insertRecords(sentDate, companyWebsite, companyName, clientEmailId, clientName, fromEmailId);
            #endregion

            #region Move processed emails into other folder
            mailRepository.MoveTOProcessedFolder("Sent", " Sent.ProcessedMails", strMsgId);
            #endregion

            divMsg.InnerHtml = "<b> <font color=\"red\"> All Emails are scanned successfully. </font> </b>";
        }
        catch (Exception ex)
        {
            btnScanEmails.Visible    = false;
            btnExportToExcel.Visible = false;
            if (ex.ToString().Contains("failed"))
            {
                divError.InnerHtml = "<b> <font color=\"red\"> LOGIN FAILED......... </font> </b>";  // +ex.ToString();
            }
            else
            {
                divError.InnerText = Convert.ToString(ex);
            }
        }
    }