public void Initialize() { _templates = new Dictionary <string, MailContent>(); DirectoryInfo directoryInfo = new DirectoryInfo(MailTemplatesDir); foreach (FileInfo file in directoryInfo.GetFiles()) { string fileContent = File.ReadAllText(Path.Combine(MailTemplatesDir, file.Name)); if (file.Name == "layout.html") { _layout = fileContent; continue; } MailContent mailContent = new MailContent { subject = ParseHtmlValue(fileContent, "Subject"), template = ParseHtmlValue(fileContent, "Body") }; _templates.Add(file.Name, mailContent); } string ParseHtmlValue(string rawString, string key) { var regex = new Regex($"<{key}>(.+)?<\\/{key}>", RegexOptions.Singleline); return(regex.Match(rawString).Groups[1].Value.Trim()); } }
public MailContent BuildMessage(string templateName, Dictionary <string, string> replaceDictionary) { if (!Templates.ContainsKey(templateName)) { throw new Exception($"Mail template {templateName} not found"); } string subject = Templates[templateName].subject; string body = Templates[templateName].template; foreach (var(key, value) in replaceDictionary) { subject = subject.Replace(key, value); body = body.Replace(key, value); } body = Layout.Replace("[content]", body); MailContent mailContent = new MailContent { subject = subject, template = body }; return(mailContent); }