public EmailSubsystem(string fromAddress, IEmailTemplateEngine templateEngine, IEmailSender sender) { Invariant.IsNotBlank(fromAddress, "fromAddress"); Invariant.IsNotNull(templateEngine, "templateEngine"); Invariant.IsNotNull(sender, "sender"); FromAddress = fromAddress; TemplateEngine = templateEngine; Sender = sender; }
public string Read(string templateName, string suffix) { Invariant.IsNotBlank(templateName, "templateName"); string content = string.Empty; string path = BuildPath(templateName, suffix); if (File.Exists(path)) { content = File.ReadAllText(path); } return(content); }
protected FileSystemEmailTemplateContentReader(string templateDirectory, string fileExtension) { Invariant.IsNotBlank(templateDirectory, "templateDirectory"); if (!Path.IsPathRooted(templateDirectory)) { templateDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, templateDirectory); } if (!Directory.Exists(templateDirectory)) { throw new DirectoryNotFoundException( string.Format(CultureInfo.CurrentCulture, "\"{0}\" does not exist.", templateDirectory)); } TemplateDirectory = templateDirectory; FileExtension = fileExtension; }
public virtual Email Execute(string templateName, object model = null) { Invariant.IsNotBlank(templateName, "templateName"); IEnumerable <KeyValuePair <string, IEmailTemplate> > templates = CreateTemplateInstances(templateName); foreach (KeyValuePair <string, IEmailTemplate> pair in templates) { pair.Value.SetModel(WrapModel(model)); pair.Value.Execute(); } var mail = new Email(); templates.SelectMany(x => x.Value.To) .Distinct(StringComparer.OrdinalIgnoreCase) .Each(email => mail.To.Add(email)); templates.SelectMany(x => x.Value.ReplyTo) .Distinct(StringComparer.OrdinalIgnoreCase) .Each(email => mail.ReplyTo.Add(email)); Action <string, Action <string> > set = (contentType, action) => { KeyValuePair <string, IEmailTemplate> item = templates.SingleOrDefault( x => x.Key.Equals(contentType)); IEmailTemplate template = item.Value; if (item.Value != null) { if (!string.IsNullOrWhiteSpace(template.From)) { mail.From = template.From; } if (!string.IsNullOrWhiteSpace(template.Sender)) { mail.Sender = template.Sender; } if (!string.IsNullOrWhiteSpace(template.Subject)) { mail.Subject = template.Subject; } template.Headers.Each(pair => mail.Headers[pair.Key] = pair.Value); if (action != null) { action(template.Body); } } }; set(ContentTypes.Text, body => { mail.TextBody = body; }); set(ContentTypes.Html, body => { mail.HtmlBody = body; }); set(string.Empty, null); return(mail); }