public void GenerateDashboard(DoSoEmail email, bool prevewOnly = false) { var dashboardXml = Dashboard?.Xml; if (string.IsNullOrEmpty(dashboardXml)) { return; } var width = DashboardWidth ?? 1000; var height = DashboardHeight ?? 1000; using (var viewver = new DashboardViewer()) { viewver.Dock = System.Windows.Forms.DockStyle.Fill; using (var ms = new MemoryStream()) { using (var sr = new StreamWriter(ms, Encoding.UTF8)) { var doc = new XmlDocument(); doc.LoadXml(dashboardXml); var definitionXml = doc.OuterXml; sr.Write(definitionXml); sr.Flush(); ms.Position = 0; try { viewver.LoadDashboard(ms); } catch (Exception e) { throw; } viewver.Size = new System.Drawing.Size(width, height); if (!prevewOnly) { var path = Path.Combine(email.FolderPath, "Dashboard" + ".png"); viewver.ExportToImage(path); //email.SourceFilePath += path + ";"; } else { using (var form = new DevExpress.XtraEditors.XtraForm()) { form.Width = width; form.Height = height; viewver.Parent = form; form.ShowDialog(); } } } } } }
public override List <DoSoMessageBase> GenerateMessages(Session session, bool prevewOnly = false) { base.GenerateMessages(session); //var itemsList = GetObjectsFromDataSource(); var item = this; var properties = session.GetProperties(/*itemsList.FirstOrDefault()*/ item.ClassInfo); var emails = new List <DoSoMessageBase>(); //foreach (var item in itemsList) { var email = new DoSoEmail(session) { EmailTo = new ExpressionEvaluator(properties, MessageTo)?.Evaluate(item)?.ToString(), EmailCC = new ExpressionEvaluator(properties, MessageCC)?.Evaluate(item)?.ToString(), EmailSubject = new ExpressionEvaluator(properties, MessageSubject)?.Evaluate(item)?.ToString(), EmailBody = new ExpressionEvaluator(properties, MessageBody)?.Evaluate(item)?.ToString(), ObjectKey = new ExpressionEvaluator(properties, ObjectKeyExpression)?.Evaluate(item)?.ToString(), ObjectTypeName = new ExpressionEvaluator(properties, ObjectTypeExpression)?.Evaluate(item)?.ToString(), SendingDate = Convert.ToDateTime(new ExpressionEvaluator(properties, SendingDateExpression)?.Evaluate(item)), ExportFileFormat = ExportFileFormat, DoSoReportSchedule = session.GetObjectByKey <DoSoReportSchedule>(ID) }; if (SkipExecutionDate != null) { var sameEmail = DoSoEmailsCollection.Where(x => x.ExpiredOn == null && x.Status == DoSoMessageBase.MessageStatusEnum.Sent && email.SendingDate.AddDays(SkipExecutionDate ?? 0) > x.SentDate && x.EmailTo == email.EmailTo); if (sameEmail.Any()) { email.CancelMessage("Skipped", DoSoMessageBase.MessageStatusEnum.Skipped); email.Delete(); } } if (!prevewOnly) { var folderPath = Path.Combine(HS.GeteratedFilesName, HS.MyTempName); email.FolderPath = folderPath; Directory.CreateDirectory(folderPath); GenerateExcelReport(email, item, session, folderPath); GenerateReportData(email, false); GenerateDashboard(email); } emails.Add(email); } return(emails); }
public void ExportRportData(DoSoEmail email, XtraReport report) { if (email.ExportFileFormat == ReportExportFileFormatEnum.HTML) { report.ExportToHtml(Path.Combine(email.FolderPath, "HtmlContent")); } if (email.ExportFileFormat == ReportExportFileFormatEnum.PDF) { var filePath = Path.Combine(email.FolderPath, HS.MyTempName + ".Pdf"); report.ExportToPdf(filePath); email.SourceFilePath += filePath + ";"; } if (email.ExportFileFormat == ReportExportFileFormatEnum.Xlsx) { var filePath = Path.Combine(email.FolderPath, HS.MyTempName + ".Xlsx"); report.ExportToXlsx(filePath); email.SourceFilePath += filePath + ";"; } }
public void GenerateReportData(DoSoEmail email, bool prevewOnly) { if (string.IsNullOrWhiteSpace(ReportDataXml)) { return; } CreateDataSourceFromXml(); ExcelDataSource?.Fill(); SqlDataSource?.Fill(); using (var report = new XtraReport()) { AddDataSource2Report(report); using (var ms = new MemoryStream()) { using (var sr = new StreamWriter(ms, Encoding.Default)) { var doc = new XmlDocument(); doc.LoadXml(ReportDataXml); var definitionXml = doc.OuterXml; sr.Write(definitionXml); sr.Flush(); ms.Position = 0; report.LoadLayoutFromXml(ms); report.FilterString = $"{ObjectKeyExpression} == {email.ObjectKey}"; report.ApplyFiltering(); //report.FillDataSource(); if (prevewOnly) { using (ReportPrintTool printTool = new ReportPrintTool(report)) printTool.ShowRibbonPreviewDialog(UserLookAndFeel.Default); } else { ExportRportData(email, report); } } } } }
public void GenerateExcelReport(DoSoEmail email, object item, Session session, string folderPath) { var xml = Report?.Xml; if (!string.IsNullOrEmpty(xml)) { var control = new SpreadsheetControl(); using (var ms = new MemoryStream(Convert.FromBase64String(xml))) control.LoadDocument(ms, DocumentFormat.OpenXml); if (control.Document.MailMergeParameters.Any()) { throw new InvalidOperationException("Need Parameters"); } var docs = control.Document.GenerateMailMergeDocuments(); foreach (var doc in docs) { var fullName = Path.Combine(folderPath, HS.MyTempName + ".Xlsx"); doc.SaveDocument(fullName); email.SourceFilePath += fullName + ";"; } } //if (ReportDefinition != null) //{ // var fullName = Path.Combine(folderPath, HS.MyTempName + ".Xlsx"); // SetParameteValues(this, item, session); // var newFileaName = FastExportingMethod.ExportData2Excel(ReportDefinition, session, fullName); // if (!string.IsNullOrEmpty(newFileaName)) // email.SourceFilePath += newFileaName + ";"; // else // email.EmailBody += string.Format("{0}{0}{1}", Environment.NewLine, AlternativeText); //} }
public static DoSoEmail GenerateEmail(UnitOfWork unitOfWork, object item, string messageSubject, string to, string cc, string messageBody, DoSoReportSchedule schedule = null) { var classInfo = unitOfWork.GetClassInfo(item); var properties = unitOfWork.GetProperties(classInfo); var mailTo = new ExpressionEvaluator(properties, to).Evaluate(item)?.ToString(); if (string.IsNullOrEmpty(mailTo)) { return(null); } var subject = new ExpressionEvaluator(properties, messageSubject).Evaluate(item)?.ToString(); var body = new ExpressionEvaluator(properties, messageBody).Evaluate(item)?.ToString(); var key = classInfo.KeyProperty.GetValue(item).ToString(); var sameMailFromDb = unitOfWork.Query <DoSoEmail>().Where(x => x.ExpiredOn == null && x.EmailTo == mailTo && x.ObjectKey == key && x.ObjectTypeName == item.GetType().FullName).ToList(); var oldNotSentMail = sameMailFromDb.Where(x => !x.IsSent && !x.IsCanceled && x.SendingDate < DateTime.Now && x.DoSoReportSchedule == schedule); foreach (var oldMail in oldNotSentMail) { oldMail.IsCanceled = true; oldMail.StatusComment = "Created New Email"; } //var alredySentMails = sameMailFromDb.FirstOrDefault(x => // x.IsSent && // !x.IsCanceled && // x.SentDate.AddDays(schedule.SkipExecutionDate) > DateTime.Now); var email = new DoSoEmail(unitOfWork) { EmailTo = mailTo, EmailSubject = subject, EmailCC = cc, EmailBody = body, SendingDate = DateTime.Now, ObjectKey = key, ObjectTypeName = item.GetType().FullName, //SourceFilePath = path + fullName, DoSoReportSchedule = schedule }; var folderName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GeneratedFiles"); if (schedule?.ReportDefinition != null) { //var path = System.IO.Path.GetTempPath(); if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } var name = DateTime.Now.ToString("MMMddHHmmssfff"); var fullName = name + ".xlsx"; SetParameteValues(schedule, item, unitOfWork); //var providerType = (unitOfWork.DataLayer as SimpleDataLayer)?.ConnectionProvider; var exported = false; //if (providerType is MSSqlConnectionProvider) //exported = FastExportingMethod.ExportData2Excel(schedule.ReportDefinition, /*unitOfWork.Connection as SqlConnection*/unitOfWork, folderName, name); //else // exported = FastExportingMethod.ExportData2ExcelFromPostgre(schedule.ReportDefinition, unitOfWork.Connection as SqlConnection, folderName, name); if (exported) { email.SourceFilePath += Path.Combine(folderName, fullName) + ";"; } else { email.EmailBody += string.Format("{0}{0}{1}", Environment.NewLine, schedule.AlternativeText); } } if (!string.IsNullOrEmpty(schedule?.AttachedFilesExpression)) { try { var data = TryGetFileData(unitOfWork, item as XPBaseObject, schedule.AttachedFilesExpression.Replace("'", ""), 0); if (data != null) { if (!Directory.Exists(folderName)) { Directory.CreateDirectory(folderName); } foreach (var fileData in data) { var extention = Path.GetExtension(fileData.FileName); var name = DateTime.Now.ToString("ddHHmmssfff"); var newFileName = $"{fileData.FileName.Replace(extention, "")}_{name}{extention}"; var fullPath = Path.Combine(folderName, newFileName); using (var stream = new FileStream(fullPath, FileMode.Create)) { fileData.SaveToStream(stream); email.SourceFilePath += fullPath + ";"; } } } } catch (Exception ex) { CreateLogFileWithException(ex.ToString()); } } return(email); }
public static void Send(DoSoEmail email, UnitOfWork unitOfWork) { try { using (var mail = new MailMessage()) { try { var fullPath = Path.Combine(email.FolderPath, "HtmlContent"); var strHTML = File.ReadAllText(fullPath); strHTML = strHTML.Replace("HtmlContent" + "_files/", ""); strHTML = strHTML.Replace("src=\"", "src=cid:"); strHTML = strHTML.Replace("gif\"", "gif"); strHTML = strHTML.Replace("png\"", "png"); strHTML = strHTML.Replace("jpg\"", "jpg"); var avHtml = AlternateView.CreateAlternateViewFromString(strHTML, Encoding.Unicode, MediaTypeNames.Text.Html); mail.AlternateViews.Add(avHtml); var di = new DirectoryInfo(fullPath + "_files"); var files = di.GetFiles("*.*"); foreach (var item in files) { var fileMimeType = GetMimeType(item.Name); //using (var pic1 = new LinkedResource(String.Format("{0}\\{1}", di.FullName, item.Name), new ContentType(fileMimeType))) //{ var pic1 = new LinkedResource($"{di.FullName}\\{item.Name}", new ContentType(fileMimeType)) { ContentId = item.Name }; avHtml.LinkedResources.Add(pic1); //} } } catch (Exception) { } if (!string.IsNullOrEmpty(email.SourceFilePath)) { var paths = email.SourceFilePath.Split(';'); foreach (var s in paths) { if (s.Length < 10) { continue; } var attachment = new Attachment(s); mail.Attachments.Add(attachment); } } mail.From = new MailAddress(HS.EMailFrom); var tos = email.EmailTo.Split(';'); foreach (var item in tos.Where(x => x.Length > 2)) { mail.To.Add(new MailAddress(item.Trim())); } var ccs = email?.EmailCC?.Split(';'); if (ccs != null) { foreach (var item in ccs?.Where(x => x.Length > 2)) { mail.CC.Add(new MailAddress(item.Trim())); } } mail.Subject = email.EmailSubject; mail.Body += email.EmailBody; mail.IsBodyHtml = true; var inlineLogo = new Attachment(Path.Combine(email.FolderPath, "Dashboard.png")); mail.Attachments.Add(inlineLogo); inlineLogo.ContentId = "Dashboard"; mail.Body += Environment.NewLine + "<htm><body> <img src=\"cid:" + "Dashboard" + "\"> </body></html>"; using (var client = new SmtpClient(HS.SmtpServer, HS.SmtpPort)) { client.EnableSsl = HS.EnableSsl; //DeliveryMethod = SmtpDeliveryMethod.Network, client.UseDefaultCredentials = HS.UseDefaultCredentials; client.Credentials = new NetworkCredential(HS.SmtpUserName, HS.SmtpPassword); client.Timeout = 50000; client.Send(mail); } email.Status = BusinessObjects.Base.DoSoMessageBase.MessageStatusEnum.Sent; email.SentDate = DateTime.Now; } } catch (Exception ex) { email.CancelMessage($"Exception Was Thrown. See Exception log ({DateTime.Now})", MessageStatusEnum.CancelledByService); HS.CreateExceptionLog(ex.Message, ex.ToString(), 5); } }
public string GenerateEmailFromSchedule(DoSoReportSchedule schedule, UnitOfWork unitOfWork) { string exception = string.Empty; var objects = SenderHelper.GetMyObjects(schedule.ObjectsCriteria, unitOfWork, schedule.BusinessObjectFullName, true, 500); foreach (var item in objects) { try { var classInfo = unitOfWork.GetClassInfo(item); var properties = unitOfWork.GetProperties(classInfo); var mailTo = new ExpressionEvaluator(properties, schedule.MessageTo).Evaluate(item).With(x => x.ToString()); if (string.IsNullOrEmpty(mailTo)) { continue; } var subject = new ExpressionEvaluator(properties, schedule.MessageSubject).Evaluate(item).ToString(); var body = new ExpressionEvaluator(properties, schedule.MessageBody).Evaluate(item).ToString(); var key = classInfo.KeyProperty.GetValue(item).ToString(); var sameMailFromDb = unitOfWork.Query <DoSoEmail>().Where(x => x.ExpiredOn == null && x.EmailTo == mailTo && x.ObjectKey == key && x.ObjectTypeName == item.GetType().FullName&& x.ReportSchedule == schedule); var oldNotSentMail = sameMailFromDb.Where(x => !x.IsSent && !x.IsCanceled && x.SendingDate < DateTime.Now); foreach (var oldMail in oldNotSentMail) { oldMail.IsCanceled = true; oldMail.StatusComment = "Created New Email"; } //var alredySentMails = sameMailFromDb.FirstOrDefault(x => // x.IsSent && // !x.IsCanceled && // x.SentDate.AddDays(schedule.SkipExecutionDate) > DateTime.Now); var email = new DoSoEmail(unitOfWork) { EmailTo = mailTo, EmailSubject = subject, EmailBody = body, SendingDate = DateTime.Now, ObjectKey = key, ObjectTypeName = item.GetType().FullName, //SourceFilePath = path + fullName, ReportSchedule = schedule }; if (schedule.ReportDefinition != null) { var path = System.IO.Path.GetTempPath(); var name = DateTime.Now.ToString("MMMddHHmmssfff"); var fullName = name + ".xlsx"; SetParameteValues(schedule, item, unitOfWork); var exported = FastExportingMethod.ExportData2Excel(schedule.ReportDefinition, unitOfWork.Connection as SqlConnection, path, name); if (exported) { email.SourceFilePath = path + fullName; } else { email.EmailBody += string.Format("{0}{0}{1}", Environment.NewLine, schedule.AlternativeText); } } schedule.GetNextExecutionDate(); unitOfWork.CommitChanges(); } catch (Exception ex) { exception += ex + Environment.NewLine; continue; } } return(exception); }