public static void SendMail_RequestUpdateCMS(KMailInfo kMailInfo) { var kMailModel = KMail.SendMail(kMailInfo); KWSLogRequestUpdateCMS kWSLogUpdateCMS = new KWSLogRequestUpdateCMS { KMailModel = kMailModel, Comment = kMailInfo.Reason }; KWSLog kWSLog = new KWSLog { EngNum = kMailInfo.EngNum, KWSLogType = KWSLogType.RequestUpdateCMS, Info = KSerDSer.JSerialize<KWSLogRequestUpdateCMS>(kWSLogUpdateCMS), }; IMWrapWSLog.KWSLog(kWSLog); //Task task = new Task(() => //{ // var kMailModel = KMail.SendMail(kMailInfo); // KWSLogRequestUpdateCMS kWSLogUpdateCMS = new KWSLogRequestUpdateCMS // { // KMailModel = kMailModel, // Comment = kMailInfo.Reason // }; // KWSLog kWSLog = new KWSLog // { // EngNum = kMailInfo.EngNum, // KWSLogType = KWSLogType.RequestUpdateCMS, // Info = KSerDSer.JSerialize<KWSLogRequestUpdateCMS>(kWSLogUpdateCMS), // }; // IMWrapWSLog.KWSLog(kWSLog); //},TaskCreationOptions.AttachedToParent //); //task.Start(TaskScheduler.FromCurrentSynchronizationContext()); }
public static void SendMail_CreateWS(KMailInfo kMailInfo, CreateWSModel createWSModel) { var kMailModel = KMail.SendMail(kMailInfo); KWSLogCreateWS kWSLogCreateWS = new KWSLogCreateWS { KMailModel = kMailModel, CreateWSModel = createWSModel, }; KWSLog kWSLog = new KWSLog { EngNum = createWSModel.EngNum, KWSLogType = createWSModel.IsAuto.GetValueOrDefault(true) ? KWSLogType.CreateWSAuto : KWSLogType.CreateWSManual, Info = KSerDSer.JSerialize<KWSLogCreateWS>(kWSLogCreateWS), }; IMWrapWSLog.KWSLog(kWSLog); //Task task = new Task(() => //{ // var kMailModel = KMail.SendMail(kMailInfo); // KWSLogCreateWS kWSLogCreateWS = new KWSLogCreateWS // { // KMailModel = kMailModel, // CreateWSModel = createWSModel, // }; // KWSLog kWSLog = new KWSLog // { // EngNum = createWSModel.EngNum, // KWSLogType = createWSModel.IsAuto.GetValueOrDefault(true) ? KWSLogType.CreateWSAuto : KWSLogType.CreateWSManual, // Info = KSerDSer.JSerialize<KWSLogCreateWS>(kWSLogCreateWS), // }; // IMWrapWSLog.KWSLog(kWSLog); //}); //task.Start(TaskScheduler.FromCurrentSynchronizationContext()); }
public static KMailModel SendMail(KMailInfo kMailInfo) { var kMailModel = ReadEmailJson(kMailInfo.KMailType); StringBuilder body = null; List<LinkedResource> linkedResource = new List<LinkedResource>(); using (StreamReader reader = new StreamReader(string.Format("{0}{1}", KConfig.GetEMailTemplatePath, kMailInfo.KMailType.ToEnumAttr<KMailType, DescriptionAttribute>().Description), System.Text.Encoding.Default)) { body = new StringBuilder(reader.ReadToEnd()); var allImage = Directory.EnumerateFiles(KConfig.GetEMailImgPath).ToList(); var noSpace = Regex.Replace(body.ToString(), @"\s+", ""); Regex regex = new Regex("<divclass=\"imageUsed\">(.*?)</div>"); var imageUsed = regex.Match(noSpace).Groups[1].ToString(); body.Replace(imageUsed, ""); imageUsed.Split(',').ToList().ForEach(x => { var imgPath = allImage.Find(y => Path.GetFileNameWithoutExtension(y).IsEquals(x)); var lR = new LinkedResource(imgPath); lR.ContentId = Guid.NewGuid().ToString(); body.Replace("{{" + Path.GetFileNameWithoutExtension(imgPath) + "}}", lR.ContentId); linkedResource.Add(lR); }); } MailMessage mail = new MailMessage(); if (KConfig.IsTestMail) { mail.From = new MailAddress(KConfig.TestFromMailId); mail.To.Add(KConfig.TestToMailId); if (!KConfig.TestToMailId.IsEquals(KConfig.GetCurrentUser.ToKPMGEmail())) mail.To.Add(KConfig.GetCurrentUser.ToKPMGEmail()); } else { mail.From = new MailAddress(kMailModel.From_Email); if (kMailInfo.KMailType == KMailType.RequestUpdateCMS) { mail.To.Add(kMailModel.To); mail.CC.Add(string.Format("{0},{1}", string.Join(",", kMailInfo.To.Select(x => x.Value.Key).ToList()), kMailModel.Support_Email)); } else { mail.To.Add(string.Join(",", kMailInfo.To.Select(x => x.Value.Key).Distinct().ToList())); mail.CC.Add(kMailModel.Support_Email); } } if (!string.IsNullOrWhiteSpace(KConfig.BccMail)) mail.Bcc.Add(KConfig.BccMail); mail.Subject = kMailModel.Subject; mail.Subject = mail.Subject.Replace("{{engNum}}", kMailInfo.EngNum); mail.Subject = mail.Subject.Replace("{{engName}}", kMailInfo.EngName); if (!KConfig.Env.IsEquals("prod")) mail.Subject = string.Format("{0} -- [Environment - {1}]", mail.Subject, KConfig.Env); body.Replace("{{engNum}}", kMailInfo.EngNum); body.Replace("{{engName}}", kMailInfo.EngName); body.Replace("{{clientName}}", kMailInfo.ClientName); body.Replace("{{currentUser}}", kMailInfo.Requestor.Value.ToName()); var manager = kMailInfo.To.Find(x => x.Key == UserType.Manager).Value; var partner = kMailInfo.To.Find(x => x.Key == UserType.Partner).Value; var partnerAssistance = kMailInfo.To.Find(x => x.Key == UserType.PartnerAssistance).Value; body.Replace("{{Manager}}", manager.Value); body.Replace("{{Partner}}", partner.Value); body.Replace("{{PartnerAssistance}}", partnerAssistance.Value); body.Replace("{{ManagerNameNMail}}", string.Format("{0} [{1}]", manager.Value, manager.Key)); body.Replace("{{PartnerNameNMail}}", string.Format("{0} [{1}]", partner.Value, partner.Key)); body.Replace("{{Reason}}", kMailInfo.Reason); body.Replace("{{baseUrl}}", KConfig.ServerBasePathSecure); mail.Body = body.ToString(); mail.IsBodyHtml = true; AlternateView htmlView = AlternateView.CreateAlternateViewFromString(mail.Body, null, "text/html"); linkedResource.ForEach(x => htmlView.LinkedResources.Add(x)); mail.AlternateViews.Add(htmlView); using (SmtpClient client = new SmtpClient()) { try { client.Send(mail); } catch (Exception ex) { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); } } kMailModel.KMailInfo = kMailInfo; kMailModel.MailHtml = mail.Body; return kMailModel; }