public void EmailReports(Item[] itemarray, CommandItem commandItem, ScheduleItem scheduleItem)
        {
            var item = commandItem.InnerItem;
              if(item["active"] != "1") return;

              Log("Starting task");
              MultilistField mf = item.Fields["reports"];
              if (mf == null) return;
              var force = item["sendempty"] == "1";
              var filePaths = mf.GetItems().Select(i => runReport(i,force));

              var mailMessage = new MailMessage
            {
              From = new MailAddress(item["from"]),
              Subject = item["subject"],
            };
            var senders = item["to"].Split(',');
            foreach (var sender in senders)
            {
                mailMessage.To.Add(sender);
            }

              mailMessage.Body = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "text");
              mailMessage.IsBodyHtml = true;

              foreach (var path in filePaths.Where(st=>!string.IsNullOrEmpty(st)))
              {
            mailMessage.Attachments.Add(new Attachment(path));
              }
              Log("attempting to send message");
              MainUtil.SendMail(mailMessage);
              Log("task finished");
        }
        public void Process(Item[] items, Sitecore.Tasks.CommandItem commandItem, ScheduleItem schedule)
        {
            CheckboxField active = commandItem.InnerItem.Fields["active"];

            if (active.Checked)
            {
                //prepare email message
                string from    = commandItem["from"];
                string to      = commandItem["to"];
                string subject = commandItem["subject"];

                MailMessage message = new MailMessage(from, to)
                {
                    Subject = subject,
                };

                //attach reports in excel format
                MultilistField reportReferences = commandItem.InnerItem.Fields["reports"];
                foreach (Item item in reportReferences.GetItems())
                {
                    ReportItem reportItem = null;
                    Report     report     = null;
                    try
                    {
                        reportItem = new ReportItem(item);
                        report     = new Report();
                        foreach (var sItem in reportItem.Scanners)
                        {
                            report.AddScanner(sItem);
                        }
                        foreach (var vItem in reportItem.Viewers)
                        {
                            report.AddViewer(vItem);
                        }
                        foreach (var fItem in reportItem.Filters)
                        {
                            report.AddFilter(fItem);
                        }
                        report.Run();

                        //attach to mail message
                        string     tempPath      = new ASR.Export.HtmlExport(report, reportItem).SaveFile("Automated report " + reportItem.Name, "xls");
                        Attachment newAttachment = new Attachment(tempPath);
                        message.Attachments.Add(newAttachment);
                    }
                    catch (Exception ex)
                    {
                        message.Body += String.Format("An error occured while running '{0}'\n{1}\n\n", reportItem.Name, ex.ToString());
                    }
                }

                MainUtil.SendMail(message);
            }
        }
        public void EmailReports(Item[] itemarray, CommandItem commandItem, ScheduleItem scheduleItem)
        {
            var item = commandItem.InnerItem;
            if (item["active"] != "1") return;

            Log("Starting report email task");
            MultilistField mf = item.Fields["reports"];
            if (mf == null) return;
            var force = item["sendempty"] == "1";
            var filePaths = mf.GetItems().Select(i => runReport(i, force));

            MailMessage mailMessage;

            try
            {
                mailMessage = new MailMessage
                {
                    From = new MailAddress(item["from"]),
                    Subject = item["subject"],
                };
            }
            catch (Exception ex)
            {
                LogException("FROM email address error.");
                return;
            }

            var senders = item["to"].Split(',');
            foreach (var sender in senders)
            {
                // test that each email address is valid. Continue to the next if it isn't.
                try
                {
                    var toAddress = new MailAddress(sender);
                    mailMessage.To.Add(toAddress);
                }
                catch (Exception)
                {
                    LogException("TO email address error. " + sender);
                }
            }

            mailMessage.Body = Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "text");
            mailMessage.IsBodyHtml = true;

            foreach (var path in filePaths.Where(st => !string.IsNullOrEmpty(st)))
            {
                mailMessage.Attachments.Add(new Attachment(path));
            }

            Log("attempting to send message");
            MainUtil.SendMail(mailMessage);
            Log("task report email finished");
        }
        public void EmailReports(Item[] itemarray, CommandItem commandItem, ScheduleItem scheduleItem)
        {
            var item = commandItem.InnerItem;
            if (item["active"] != "1") return;

            Log("Starting report email task");
            MultilistField mf = item.Fields["reports"];
            if (mf == null) return;
            var force = item["sendempty"] == "1";

            var isHtmlExportType = item["Export Type"].ToLower() == "html";

            var filePaths = mf.GetItems().Select(i => runReport(i, force, isHtmlExportType));

            MailMessage mailMessage;

            try
            {
                mailMessage = new MailMessage
                {
                    From = new MailAddress(item["from"]),
                    Subject = setDate(item["subject"]),
                };
            }
            catch (Exception)
            {
                LogException("FROM email address error." + item["from"]);
                return;
            }

            var senders = item["to"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var sender in senders)
            {
                // test that each email address is valid. Continue to the next if it isn't.
                try
                {
                    var toAddress = new MailAddress(sender);
                    mailMessage.To.Add(toAddress);
                }
                catch (Exception)
                {
                    LogException("TO email address error. " + sender);
                }
            }

            string[] ccAddressList = item["cc"].Split(new[]
            {
                ','
            }, StringSplitOptions.RemoveEmptyEntries);

            if (ccAddressList.Any())
            {
                foreach (var ccAddress in ccAddressList)
                {
                    try
                    {
                        // test that each email address is valid. Continue to the next if it isn't.
                        MailAddress ccMailAddress = new MailAddress(ccAddress);
                        mailMessage.CC.Add(ccMailAddress);
                    }
                    catch (Exception)
                    {
                        LogException("CC email address error. " + ccAddress);
                    }
                }
            }

            string[] bccAddressList = item["bcc"].Split(new[]
            {
                ','
            }, StringSplitOptions.RemoveEmptyEntries);

            if (bccAddressList.Any())
            {
                foreach (var bccAddress in bccAddressList)
                {
                    try
                    {
                        // test that each email address is valid. Continue to the next if it isn't.
                        MailAddress bccMailAddress = new MailAddress(bccAddress);
                        mailMessage.Bcc.Add(bccMailAddress);
                    }
                    catch (Exception)
                    {
                        LogException("BCC email address error. " + bccAddress);
                    }
                }
            }

            mailMessage.Body = setDate(Sitecore.Web.UI.WebControls.FieldRenderer.Render(item, "text"));
            mailMessage.IsBodyHtml = true;

            foreach (var path in filePaths.Where(st => !string.IsNullOrEmpty(st)))
            {
                mailMessage.Attachments.Add(new Attachment(path));
            }

            Log("attempting to send message");
            MainUtil.SendMail(mailMessage);
            Log("task report email finished");
        }