示例#1
0
        protected void btnDocumentsSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtKeyword.Text))
            {
                lblErrorMSg.Visible = true;
            }
            else
            {
                lblErrorMSg.Visible = false;
                int userID = SessionHelper.getUserId();

                CRM.Data.Entities.SecUser secUser = SecUserManager.GetByUserId(userID);
                string email    = secUser.Email;
                string password = SecurityManager.Decrypt(secUser.emailPassword);
                string url      = "https://" + secUser.emailHost + "/EWS/Exchange.asmx";

                try
                {
                    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                    service.Credentials = new WebCredentials(email, password);
                    service.Url         = new Uri(url);

                    ItemView view = new ItemView(int.MaxValue);

                    Microsoft.Exchange.WebServices.Data.SearchFilter searchFilter = new Microsoft.Exchange.WebServices.Data.SearchFilter.ContainsSubstring(ItemSchema.Body, txtKeyword.Text);
                    instanceResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

                    DataTable table = GetTable();

                    foreach (var item in instanceResults.Items)
                    {
                        item.Load();
                        //Console.Clear();
                        EmailMessage msg = item as EmailMessage;
                        DataRow      row = table.NewRow();

                        string to = string.Empty;
                        foreach (var rec in msg.ToRecipients)
                        {
                            to = to + rec.Address + ";";
                        }

                        row["Subject"] = msg.Subject;
                        string body = ExtractHtmlInnerText(msg.Body);
                        if (body.Length > 2000)
                        {
                            body = body.Substring(0, 2000);
                        }
                        row["Body"] = "From :" + msg.From.Address + "\nTo :" + to + "\nSubject: " + msg.Subject + "\n" + body;
                        row["No.of Attachments"] = msg.Attachments.Count.ToString();
                        row["Date"] = msg.DateTimeReceived.Date.ToShortDateString();
                        table.Rows.Add(row);
                    }
                    gvSearchResult.DataSource = table;
                    gvSearchResult.DataBind();

                    pnlSearchResult.Visible = true;
                }
                catch (Exception ex)
                {
                    lblErrorMSg.Text    = "Incorrect Email Settings";
                    lblErrorMSg.Visible = true;
                }
            }
        }
        protected void btnDocumentsSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtKeyword.Text))
            {
                lblErrorMSg.Visible = true;
            }
            else
            {
                lblErrorMSg.Visible = false;
                int userID = SessionHelper.getUserId();

                CRM.Data.Entities.SecUser secUser = SecUserManager.GetByUserId(userID);
                string email = secUser.Email;
                string password = SecurityManager.Decrypt(secUser.emailPassword);
                string url = "https://" + secUser.emailHost + "/EWS/Exchange.asmx";

                try
                {
                    ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                    service.Credentials = new WebCredentials(email, password);
                    service.Url = new Uri(url);

                    ItemView view = new ItemView(int.MaxValue);

                    Microsoft.Exchange.WebServices.Data.SearchFilter searchFilter = new Microsoft.Exchange.WebServices.Data.SearchFilter.ContainsSubstring(ItemSchema.Body, txtKeyword.Text);
                    instanceResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

                    DataTable table = GetTable();

                    foreach (var item in instanceResults.Items)
                    {
                        item.Load();
                        //Console.Clear();
                        EmailMessage msg = item as EmailMessage;
                        DataRow row = table.NewRow();

                        string to = string.Empty;
                        foreach (var rec in msg.ToRecipients)
                            to = to + rec.Address + ";";

                        row["Subject"] = msg.Subject;
                        string body = ExtractHtmlInnerText(msg.Body);
                        if (body.Length > 2000)
                            body = body.Substring(0, 2000);
                        row["Body"] = "From :" + msg.From.Address + "\nTo :" + to + "\nSubject: " + msg.Subject + "\n" + body;
                        row["No.of Attachments"] = msg.Attachments.Count.ToString();
                        row["Date"] = msg.DateTimeReceived.Date.ToShortDateString();
                        table.Rows.Add(row);
                    }
                    gvSearchResult.DataSource = table;
                    gvSearchResult.DataBind();

                    pnlSearchResult.Visible = true;
                }
                catch (Exception ex)
                {
                    lblErrorMSg.Text = "Incorrect Email Settings";
                    lblErrorMSg.Visible = true;
                }

            }
        }