Пример #1
0
        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);
        }
Пример #2
0
        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);
            }
        }