Beispiel #1
0
        public EmailTemplateModelWrapper(IDictionary <string, object> propertyMap)
        {
            Invariant.IsNotNull(propertyMap, "propertyMap");

            this.propertyMap = propertyMap;
        }
Beispiel #2
0
        public SmtpClientWrapper(SmtpClient realClient)
        {
            Invariant.IsNotNull(realClient, "realClient");

            this.realClient = realClient;
        }
Beispiel #3
0
        public virtual Email Execute(string templateName, object model = null)
        {
            Invariant.IsNotBlank(templateName, "templateName");
            templateName = templateName.CleanCodeName();
            var templates = CreateTemplateInstances(templateName);

            foreach (var pair in templates)
            {
                pair.Value.SetModel(model);
                pair.Value.Execute();
            }

            var mail = new Email();

            templates.SelectMany(x => x.Value.To)
            .Distinct(StringComparer.OrdinalIgnoreCase)
            .Each(AddMails(mail));

            templates.SelectMany(x => x.Value.ReplyTo)
            .Distinct(StringComparer.OrdinalIgnoreCase)
            .Each(AddReplyToMails(mail));

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

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

            Action <string, Action <string> > set = (contentType, action) => {
                var            item     = templates.SingleOrDefault(x => x.Key.Equals(contentType) && x.Value.GetType().Name.EndsWith(BodyTemplateNameSuffix));
                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);
                    }
                }
            };

            foreach (var type in new[] { ContentTypes.Text, ContentTypes.Html, string.Empty })
            {
                set(type, AssignTextBody(mail));
                set(type, AssignHtmlBody(mail));
                var valuePair = templates.SingleOrDefault(x => x.Key.Equals(type) && x.Value.GetType().Name.EndsWith(SubjectTemplateNameSuffix));
                if (valuePair.Value != null)
                {
                    mail.Subject = valuePair.Value.Body;
                }
            }
            return(mail);
        }
Beispiel #4
0
        public void Send(MailMessage message)
        {
            Invariant.IsNotNull(message, "message");

            realClient.Send(message);
        }
Beispiel #5
0
        public virtual Email Execute(string templateName, object model = null)
        {
            Invariant.IsNotBlank(templateName, "templateName");

            var templates = CreateTemplateInstances(templateName);

            foreach (var 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));

            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);

            return(mail);
        }