public void Error(string message, string stracktrace = "", string module = "", UserCredential user = null, string token = "") { try { using (var scope = new TransactionScope(TransactionScopeOption.Suppress)) { var u = UserUtilities.GetUser(); var log = new LogMessage(); log.Message = message; log.Level = EnumLogLevel.Error.ToString(); log.CreateBy = user != null ? user.UserId : ""; log.Token = token; log.Module = module; log.StackTrace = stracktrace; log.CreateDate = DateTime.Now; if (u != null) { log.CreateBy = u.UserId; log.Token = UserUtilities.GetToken(); } _db.LogMessages.Add(log); _db.SaveChanges(); scope.Complete(); } } catch (Exception e) { WriteLogLocal(message, stracktrace, module); } }
public static async Task SendEmailWithBodyHTMLAsync(EmailModel model, bool isChemDailyReport = false) { DomainEvents._Container.Resolve <ILogRepository>().Info(model.ToJSON(), "SendEmailWithBodyHTMLAsync", UserUtilities.GetUser(), UserUtilities.GetToken()); if (model.Email_Tos.IsHasValue() == false) { return; } SmtpClient client = new SmtpClient(); //client.Port = AppConfigUtilities.GetAppConfig<int>("SMTP_PORT"); //client.Host = ConfigurationManager.AppSettings["SMTP_HOST"]; client.Port = APPCONSTANT.ConfigSendEmail.SMTP_PORT; client.Host = APPCONSTANT.ConfigSendEmail.SMTP_HOST; client.Timeout = 10000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; MailMessage msg = new MailMessage(); msg.From = new MailAddress(AppConfigUtilities.GetAppConfig <string>("FREIGTHX_MAIL_FROM")); //var recipients = model.Email_To.Split(';'); if (model.Email_Tos.IsHasValue()) { foreach (var recipient in model.Email_Tos.Where(x => string.IsNullOrEmpty(x) == false)) { msg.To.Add(new MailAddress(recipient)); } } if (model.CCs.IsHasValue()) { //var ccsList = model.CCs.Split(';'); foreach (var cc in model.CCs.Where(x => string.IsNullOrEmpty(x) == false)) { msg.CC.Add(new MailAddress(cc)); } } msg.IsBodyHtml = true; msg.Subject = model.Subject; msg.Body = model.Body; #region Send File Or PDF if (model.binary != null && model.binary.Any() && !string.IsNullOrEmpty(model.FileName)) { Attachment att = new Attachment(new MemoryStream(model.binary), model.FileName); msg.Attachments.Add(att); } #endregion if (isChemDailyReport == false) { AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(model.Body, null, MediaTypeNames.Text.Html); //Add logo alternativeView.ContentId = "htmlView"; alternativeView.TransferEncoding = TransferEncoding.SevenBit; var logo = GetEmbeddedImage("logo", "", "Assets/images/scglogo1.png"); alternativeView.LinkedResources.Add(logo); msg.AlternateViews.Add(alternativeView); //End logo } await client.SendMailAsync(msg); //DomainEvents._Container.Resolve<ILogRepository>().Info(msg.ToJSON(), "SendEmailWithBodyHTMLAsync", UserUtilities.GetUser(), UserUtilities.GetToken()); }
public static void SendEmailWithBodyHTML(EmailModel model) { DomainEvents._Container.Resolve <ILogRepository>().Info(model.ToJSON(), "SendEmailWithBodyHTML", UserUtilities.GetUser(), UserUtilities.GetToken()); if (model.Email_Tos == null || model.Email_Tos.Any() == false) { return; } SmtpClient client = new SmtpClient(); //client.Port = AppConfigUtilities.GetAppConfig<int>("SMTP_PORT"); //client.Host = ConfigurationManager.AppSettings["SMTP_HOST"]; client.Port = APPCONSTANT.ConfigSendEmail.SMTP_PORT; client.Host = APPCONSTANT.ConfigSendEmail.SMTP_HOST; client.Timeout = 10000; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; MailMessage msg = new MailMessage(); msg.From = new MailAddress(AppConfigUtilities.GetAppConfig <string>("FREIGTHX_MAIL_FROM")); //var recipients = model.Email_To.Split(';'); if (model.Email_Tos != null && model.Email_Tos.Any() && model.Email_Tos.Count() > 0) { foreach (var recipient in model.Email_Tos) { msg.To.Add(new MailAddress(recipient)); } } if (model.CCs != null && model.CCs.Any() && model.CCs.Count() > 0) { //var ccsList = model.CCs.Split(';'); foreach (var cc in model.CCs) { msg.CC.Add(new MailAddress(cc)); } } msg.IsBodyHtml = true; msg.Subject = model.Subject; msg.Body = model.Body; AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(model.Body, null, MediaTypeNames.Text.Html); //Add logo alternativeView.ContentId = "htmlView"; alternativeView.TransferEncoding = TransferEncoding.SevenBit; var logo = GetEmbeddedImage("logo", "", "Assets/images/scglogo1.png"); alternativeView.LinkedResources.Add(logo); msg.AlternateViews.Add(alternativeView); //End logo client.Send(msg); }