Esempio n. 1
0
        private IEnumerable <KeyValuePair <string, Type> > GenerateTemplateTypes(string templateName)
        {
            var suffixesWithContentTypes = new Dictionary <string, string>
            {
                { SharedTemplateSuffix, string.Empty },
                { HtmlTemplateSuffix, ContentTypes.Html },
                { TextTemplateSuffix, ContentTypes.Text }
            };

            var templates = suffixesWithContentTypes.Select(pair => new
            {
                Suffix       = pair.Key,
                TemplateName = templateName + pair.Key,
                Content      = ContentReader.Read(templateName, pair.Key),
                ContentType  = pair.Value
            })
                            .Where(x => !string.IsNullOrWhiteSpace(x.Content))
                            .ToList();

            var compliableTemplates = templates.Select(x => new KeyValuePair <string, string>(x.TemplateName, x.Content)).ToArray();

            var assembly = GenerateAssembly(compliableTemplates);

            var result = templates.Select(x => new KeyValuePair <string, Type>(x.ContentType, assembly.GetType(NamespaceName + "." + x.TemplateName, true, false))).ToList();

            Invariant.IsNotEmpty(
                result,
                string.Format(CultureInfo.CurrentUICulture, "Templates could not be loaded, check the name supplied matches the filename, missing template was: {0}", templateName));

            result.Add(new KeyValuePair <string, Type>(String.Format("{0}.dll", assembly.FullName.Split(',')[0]),
                                                       result.FirstOrDefault().Value));
            return(result);
        }
Esempio n. 2
0
        private IEnumerable <KeyValuePair <string, Type> > GetTemplateTypes(string templateName)
        {
            IEnumerable <KeyValuePair <string, Type> > templateTypes;

            SyncLock.EnterUpgradeableReadLock();

            try
            {
                if (!TypeMapping.TryGetValue(templateName, out templateTypes))
                {
                    SyncLock.EnterWriteLock();

                    try
                    {
                        templateTypes = GenerateTemplateTypes(templateName);
                        TypeMapping.Add(templateName, templateTypes);
                    }
                    finally
                    {
                        SyncLock.ExitWriteLock();
                    }
                }
            }
            finally
            {
                SyncLock.ExitUpgradeableReadLock();
            }

            Invariant.IsNotEmpty(
                templateTypes,
                string.Format(CultureInfo.CurrentUICulture, "Templates could not be loaded, check the name supplied matches the filename, missing template was: {0}", templateName));

            return(templateTypes);
        }
Esempio n. 3
0
        public virtual Email Execute(string templateName, object model = null)
        {
            Invariant.IsNotBlank(templateName, "templateName");

            var templates = CreateTemplateInstances(templateName).ToList();

            Invariant.IsNotEmpty(templates, string.Format(CultureInfo.CurrentUICulture, "was unable to find a matching templates with name: \"{0}\" .", templateName));

            foreach (var pair in templates.Where(x => !x.Key.EndsWith(".dll")))
            {
                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));

            templates.SelectMany(x => x.Value.Bcc)
            .Distinct(StringComparer.OrdinalIgnoreCase)
            .Each(email => mail.Bcc.Add(email));

            templates.SelectMany(x => x.Value.CC)
            .Distinct(StringComparer.OrdinalIgnoreCase)
            .Each(email => mail.CC.Add(email));

            Action <string, Action <string> > set = (contentType, action) =>
            {
                var 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);

            var generatedAssemblyInfo = templates.FirstOrDefault(x => x.Key.EndsWith(".dll"));

            mail.GeneratedAssemblyName = generatedAssemblyInfo.Key;

            return(mail);
        }