Factory class for creating "mail-merged" MailMessage objects
Exemple #1
0
        public void Execute()
        {
            var factory = new MergedEmailFactory(new TemplateParser());
            var employees = _repository.FindAll<Employee>();
            var emailTemplate = _repository.Query<EmailTemplate>(x => x.Name == "Daily Tasks List").FirstOrDefault();
            employees.Each(x =>
                               {
                                   var sb = new StringBuilder();
                                   x.GetTasks().Where(t=>t.ScheduledDate >= DateTime.Now && t.ScheduledDate <= DateTime.Now.AddDays(1)).Each(task =>
                                                         {
                                                             sb.Append("Task Type: ");
                                                             sb.Append(task.TaskType.Name);
                                                             sb.AppendLine();
                                                             sb.Append("Start Time: ");
                                                             sb.Append(task.ScheduledStartTime);
                                                             sb.AppendLine();
                                                             sb.Append("Field: ");
                                                             sb.Append(task.Field.Name);
                                                             sb.AppendLine();
                                                             sb.AppendLine();
                                                         });
                                   var tokenValues = new Dictionary<string, string>
                                                         {
                                                             {"name", x.FullName},
                                                             {"date", DateTime.Now.ToLongDateString()},
                                                             {"tasks", sb.ToString()}
                                                         };

                                   MailMessage message = factory
                                       .WithTokenValues(tokenValues)
                                       .WithSubject("Daily Tasks Email")
                                       .WithHtmlBody(emailTemplate.Template)
                                       .Create();

                                   var from = new MailAddress("*****@*****.**", "Automated Emailer");
                                   var to = new MailAddress(x.Email, x.FullName);
                                   message.From = from;
                                   message.To.Add(to);

                                   var smtpClient = new SmtpClient();
                                   smtpClient.Send(message);
                               });
        }
Exemple #2
0
    public static void sendAdministratorAlert(string abstractID)
    {
        Functionality fn             = new Functionality();
        DataSet       ds             = fn.GetDatasetByCommand("select * from UploadPoster where abstractID='" + abstractID + "'", "UploadPoster");
        DataTable     dt             = ds.Tables[0];
        string        abstractId     = abstractID;
        string        posterAuthor   = dt.Rows[0]["posterAuthor"].ToString();
        string        posterTitle    = dt.Rows[0]["posterTitle"].ToString();
        string        Email          = dt.Rows[0]["email"].ToString();
        string        posterCategory = dt.Rows[0]["posterCategory"].ToString();

        Alpinely.TownCrier.TemplateParser     tm      = new Alpinely.TownCrier.TemplateParser();
        Alpinely.TownCrier.MergedEmailFactory factory = new Alpinely.TownCrier.MergedEmailFactory(tm);
        string template    = HttpContext.Current.Server.MapPath("~/template/adminAlert.html");
        var    tokenValues = new Dictionary <string, string>
        {
            { "author", posterAuthor },
            { "poster", abstractID },
            { "title", posterTitle },
            { "category", posterCategory },
            { "email", Email }
        };
        MailMessage message = factory
                              .WithTokenValues(tokenValues)
                              .WithHtmlBodyFromFile(template)
                              .Create();

        message.From = new MailAddress("*****@*****.**", "IDA2013");
        message.To.Add(new MailAddress("*****@*****.**"));
        message.CC.Add(new MailAddress("*****@*****.**"));
        message.Bcc.Add(new MailAddress("*****@*****.**"));
        SmtpClient emailClient = new SmtpClient("smtp.gmail.com");

        System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential("*****@*****.**", "c0rpm41l");
        emailClient.UseDefaultCredentials = false;
        emailClient.EnableSsl             = true;
        emailClient.Credentials           = SMTPUserInfo;
        emailClient.Send(message);
    }
        public void CanSendHtmlEmailWithPlainAlternative()
        {
            var factory = new MergedEmailFactory(new TemplateParser());

            var tokenValues = new Dictionary<string, string>
                                  {
                                      {"name", "Joe"},
                                      {"userid", "123"}
                                  };

            MailMessage message = factory
                .WithTokenValues(tokenValues)
                .WithSubject("Test Subject")
                .WithHtmlBodyFromFile(@"templates\sample-email.html")
                .WithPlainTextBodyFromFile(@"templates\sample-email.txt")
                .Create();

            var from = new MailAddress("*****@*****.**", "Automated Emailer");
            var to = new MailAddress("*****@*****.**", "Joe Bloggs");
            message.From = from;
            message.To.Add(to);

            using (var output = new MemoryStream())
            {
                message.Save(output);
                var result = StreamToString(output);

                Assert.That(result, Contains.Substring(@"Content-Type: multipart/alternative"));

                Assert.That(result, Contains.Substring(@"Dear Joe"));

                // Note that actually the email is encoded with RFC 2045 "Quoted Printable" Encoding, but .NET doesn't ship
                // with a decoder so we'll add the =3D into our test for simplicity.
                Assert.That(result, Contains.Substring(@"http://localhost/trackedLink?userId=3D123"));

                Console.WriteLine(result);
            }
        }