public void Send(Email email)
        {
            Invariant.IsNotNull(email, "email");

            using (var message = CreateDefaultMailMessageFactory()(email))
            {
                using (var client = CreateClientFactory())
                {
                    client.Send(message);
                }
            }
        }
Example #2
0
        public void Send(Email email)
        {
            Invariant.IsNotNull(email, "email element was unable to be built correctly");
            email.From = string.IsNullOrWhiteSpace(email.From) ? DefaultFromAddress : email.From;
            Invariant.IsNotNull(email.From, "The supplied 'From' address cannot be null.");

            using (var message = CreateDefaultMailMessageFactory()(email))
            {
                using (var client = CreateClientFactory())
                {
                    client.Send(message);
                }
            }
        }
        public virtual Email Execute(string templateName, object model = null)
        {
            Invariant.IsNotBlank(templateName, "templateName");

            var templates = CreateTemplateInstances(templateName);
            var imageFiles = ReadImageFiles(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);

            mail.Attachments = imageFiles;

            return mail;
        }
        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;
        }