public EmailTemplate Format(EmailTemplate template, ParameterCollection parameterCollection)
 {
     string from = this.FormatText(template.From, parameterCollection);
     string to = this.FormatText(template.To, parameterCollection);
     string subject = "=?UTF-8?B?" + Convert.ToBase64String(Encoding.UTF8.GetBytes(this.FormatText(template.Subject, parameterCollection))) + "?=";
     return new EmailTemplate(subject, from, to, this.FormatText(template.Body, parameterCollection), template.BodyContentType, template.BodyEncoding);
 }
        public void Send(EmailTemplate template, IEmailFormatter formatter, ParameterCollection parameterHash)
        {
            EmailTemplate formattedTemplate = formatter.Format(template, parameterHash);
            logger.DebugFormat(@"
                Send email: {0}
                To: {1},
                From: {2},

                {3}", formattedTemplate.Subject, formattedTemplate.To, formattedTemplate.From, formattedTemplate.Body);
        }
 /// <summary>
 /// Formats the section.
 /// </summary>
 /// <param name="sectionTemplate">The section template.</param>
 /// <param name="emailTemplate">The email template.</param>
 /// <param name="parameterHash">The parameter hash.</param>
 /// <param name="htmlEncode">if set to <c>true</c> [HTML encode].</param>
 /// <returns>String</returns>
 protected string FormatSection(string sectionTemplate, EmailTemplate emailTemplate, ParameterCollection parameterHash, bool htmlEncode)
 {
     string text = sectionTemplate;
     IDictionaryEnumerator hashEnum = parameterHash.GetEnumerator();
     while (hashEnum.MoveNext())
     {
         object key = hashEnum.Key;
         object value = hashEnum.Value;
         if (key != null && value != null) text = FormatOneField(text, emailTemplate, key.ToString(), value.ToString(), htmlEncode);
     }
     return text;
 }
 /// <summary>
 /// Formats the specified email template.
 /// </summary>
 /// <param name="emailTemplate">The email template.</param>
 /// <param name="parameterHash">The parameter hash.</param>
 /// <returns></returns>
 public EmailTemplate Format(EmailTemplate emailTemplate, ParameterCollection parameterHash)
 {
     string fromAddresses = this.FormatSection(emailTemplate.From, emailTemplate, parameterHash);
     string toAddresses = this.FormatSection(emailTemplate.To, emailTemplate, parameterHash);
     string subject = "=?UTF-8?B?" +
         Convert.ToBase64String(Encoding.UTF8.GetBytes(this.FormatSection(emailTemplate.Subject, emailTemplate, parameterHash))) +
         "?=";
     string body = this.FormatSection(emailTemplate.Body, emailTemplate, parameterHash, true);
     EmailTemplate formattedEmailTemplate = new EmailTemplate(subject, fromAddresses, toAddresses, body, emailTemplate.BodyContentType, emailTemplate.BodyEncoding);
     logger.Debug("Formatted email template: " + formattedEmailTemplate);
     return formattedEmailTemplate;
 }
        public virtual void Send(EmailTemplate template, IEmailFormatter formatter, ParameterCollection parameterHash)
        {
            EmailTemplate formattedEmailTemplate = formatter.Format(template, parameterHash);
            MailMessage mailMessage = new MailMessage(formattedEmailTemplate.From, formattedEmailTemplate.To,
                formattedEmailTemplate.Subject, formattedEmailTemplate.Body);

            mailMessage.IsBodyHtml = !String.IsNullOrEmpty(formattedEmailTemplate.BodyContentType) && formattedEmailTemplate.BodyContentType.Equals("text/html");

            try
            {
                smtpClient.Send(mailMessage);
            }
            catch (Exception ex)
            {
                logger.Error("Error on sending mail: " + ex);
                throw new EmailSenderException("System cannot send email to " + formattedEmailTemplate.To);
            }
        }
 public EmailTemplate Format(EmailTemplate template, ParameterCollection parameters)
 {
     return template;
 }
 /// <summary>
 /// Formats the section.
 /// </summary>
 /// <param name="sectionTemplate">The section template.</param>
 /// <param name="emailTemplate">The email template.</param>
 /// <param name="parameterCollection">The parameter collection.</param>
 /// <returns>String</returns>
 protected string FormatSection(string sectionTemplate, EmailTemplate emailTemplate, ParameterCollection parameterCollection)
 {
     return this.FormatSection(sectionTemplate, emailTemplate, parameterCollection, false);
 }
 /// <summary>
 /// Formats the one field.
 /// </summary>
 /// <param name="sectionTemplate">The section template.</param>
 /// <param name="emailTemplate">The email template.</param>
 /// <param name="fieldName">Name of the field.</param>
 /// <param name="fieldValue">The field value.</param>
 /// <param name="htmlEncode">if set to <c>true</c> [HTML encode].</param>
 /// <returns>String</returns>
 protected string FormatOneField(string sectionTemplate, EmailTemplate emailTemplate, string fieldName, string fieldValue, bool htmlEncode)
 {
     string token = string.Format("${{{0}}}", fieldName);
     string value = emailTemplate.BodyContentType.Equals("text/html") && htmlEncode ? System.Web.HttpUtility.HtmlEncode(fieldValue).Replace("\n", "<br />") : fieldValue;
     return sectionTemplate.Replace(token, value);
 }