Esempio n. 1
0
        } //ctor

        public void Execute(IReportTaskRunContext taskContext)
        {
            var package = taskContext.Packages[Properties.PackageName];

            if (!RunIfVoidPackage && package.DataSets.Count == 0)
            {
                return;
            }

            string filename = (string.IsNullOrEmpty(ReportName)
                                  ? $@"{Properties.PackageName}"
                                  : taskContext.SetStringParameters(ReportName))
                              + (DateInName
                                  ? null
                                  : $" {DateTime.Now:dd.MM.yy}");

            string filenameJson = $@"{filename}.json";
            string filenameXlsx = $@"{filename}.xlsx";

            using (var client = new SmtpClient(ConfigurationManager.AppSettings["SMTPServer"], 25))
                using (var msg = new MailMessage())
                {
                    client.DeliveryFormat = SmtpDeliveryFormat.International;
                    client.EnableSsl      = true;
                    client.DeliveryMethod = SmtpDeliveryMethod.Network;

                    msg.From = new MailAddress(ConfigurationManager.AppSettings["from"]);
                    msg.AddRecepientsFromGroup(addresses);

                    if (!string.IsNullOrEmpty(RecepientsDatasetName))
                    {
                        msg.AddRecepientsFromPackage(taskContext.Packages[RecepientsDatasetName]);
                    }

                    msg.Subject = filename;

                    var dataset = parser.GetPackageValues(package).First();

                    viewExecutor = dataset.GroupColumns != null && dataset.GroupColumns.Any()
                    ? autofac.ResolveNamed <IViewExecutor>("GroupedViewex")
                    : !string.IsNullOrEmpty(ViewTemplate)
                        ? autofac.ResolveNamed <IViewExecutor>("CommonTableViewEx")
                        : autofac.ResolveNamed <IViewExecutor>("commonviewex");

                    if (HasHtmlBody)
                    {
                        msg.IsBodyHtml = true;
                        msg.Body       = viewExecutor.ExecuteHtml(ViewTemplate, package);
                    }

                    MemoryStream streamJson = null;
                    MemoryStream streamXlsx = null;

                    try
                    {
                        if (HasJsonAttachment)
                        {
                            var sets       = parser.GetPackageValues(package);
                            var dataToSave = UseAllSetsJson
                            ? JsonConvert.SerializeObject(sets)
                            : JsonConvert.SerializeObject(sets.First());

                            streamJson =
                                new MemoryStream(System.Text.Encoding.UTF8
                                                 .GetBytes(dataToSave));
                            msg.Attachments.Add(new Attachment(streamJson, filenameJson,
                                                               @"application/json"));
                        }

                        if (HasXlsxAttachment)
                        {
                            streamXlsx = new MemoryStream();
                            var excel = viewExecutor.ExecuteXlsx(package, filename, UseAllSetsXlsx);
                            excel.SaveAs(streamXlsx);
                            excel.Dispose();
                            streamXlsx.Position = 0;
                            msg.Attachments.Add(new Attachment(streamXlsx, filenameXlsx,
                                                               @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"));
                        }

                        var tryCount = 0;
                        while (tryCount < 3)
                        {
                            try
                            {
                                client.Send(msg);
                                break;
                            }
                            catch (Exception exc)
                            {
                                if (tryCount == 2)
                                {
                                    throw new Exception("Message not sent", exc);
                                }
                                else
                                {
                                    tryCount++;
                                }
                            }
                        }
                    }

                    finally
                    {
                        streamJson?.Dispose();

                        streamXlsx?.Dispose();
                    }
                }
        } //method