Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface IEmailGenerator
- {
- MailMessage generateEmail(IMailObject mailObject, string htmlTemplate, string textTemplate);
- }
- public class EmailGenerator : IEmailGenerator, IRegisterInIoC
- {
- private string mergeTemplate(string template, object obj)
- {
- Regex operationParser = new Regex(@"$(?:(?<operation>[w-,.]+)x20)(?<value>[w-,.]+)$", RegexOptions.Compiled);
- Regex valueParser = new Regex(@"$(?<value>[w-,.]+)$", RegexOptions.Compiled);
- var operationMatches = operationParser.Matches(template).Cast<Match>().Reverse().ToList();
- foreach (var match in operationMatches)
- {
- string operation = match.Groups["operation"].Value;
- string value = match.Groups["value"].Value;
- var propertyInfo = obj.GetType().GetProperty(value);
- if (propertyInfo == null)
- throw new TillitException(String.Format("Could not find '{0}' in object of type '{1}'.", value, obj));
- object dataValue = propertyInfo.GetValue(obj, null);
- if (operation == "endforeach")
- {
- string foreachToken = "$foreach " + value + "$";
- var startIndex = template.LastIndexOf(foreachToken, match.Index);
- var templateBlock = template.Substring(startIndex + foreachToken.Length, match.Index - startIndex - foreachToken.Length);
- var items = (IEnumerable) value;
- string blockResult = "";
- foreach (object item in items)
- {
- blockResult += mergeTemplate(templateBlock, item);
- }
- template = template.Remove(startIndex, match.Index - startIndex).Insert(startIndex, blockResult);
- }
- }
- var valueMatches = valueParser.Matches(template).Cast<Match>().Reverse().ToList();
- foreach (var match in valueMatches)
- {
- string value = match.Groups["value"].Value;
- var propertyInfo = obj.GetType().GetProperty(value);
- if (propertyInfo == null)
- throw new Exception(String.Format("Could not find '{0}' in object of type '{1}'.", value, obj));
- object dataValue = propertyInfo.GetValue(obj, null);
- template = template.Remove(match.Index, match.Length).Insert(match.Index, dataValue.ToString());
- }
- return template;
- }
- public MailMessage generateEmail(IMailObject mailObject, string htmlTemplate, string textTemplate)
- {
- var mailMessage = new MailMessage();
- mailMessage.IsBodyHtml = true;
- mailMessage.Subject = mailObject.Subject;
- mailMessage.BodyEncoding = Encoding.UTF8;
- // Create the Plain Text version of the email
- mailMessage.Body = this.mergeTemplate(textTemplate, mailObject);
- // Create the HTML version of the email
- ContentType mimeType = new System.Net.Mime.ContentType("text/html");
- AlternateView alternate = AlternateView.CreateAlternateViewFromString(this.mergeTemplate(htmlTemplate, mailObject), mimeType);
- mailMessage.AlternateViews.Add(alternate);
- return mailMessage;
- }
- }
- public class MessageData : IMailObject
- {
- public string Property1 { get; private set; }
- public string Property2 { get; private set; }
- public string Property3 { get; private set; }
- public string Property4 { get; private set; }
- public string Property5 { get; private set; }
- public string Property6 { get; private set; }
- public string Subject
- {
- get { return this.Property1 + DateTime.Now.ToShortDateString(); }
- }
- public List<MessageItemData> Items { get; private set; }
- public MessageData(string property1, string property2, string property3, DateTime property4, string property7, string property8, DateTime property9, DateTime property10, int property11, double property12, string property5, string property6)
- {
- this.Property1 = property1;
- this.Property2 = property2;
- this.Property3 = property3;
- this.Property4 = property4.ToShortDateString();
- this.Property5 = property5;
- this.Property6 = property6;
- this.Items = new List<MessageItemData>();
- this.Items.Add(new MessageItemData(property7, property8, property9, property10, property11, property12));
- }
- }
- public class MessageItemData
- {
- public string Property7 { get; private set; }
- public string Property8 { get; private set; }
- public string Property9 { get; private set; }
- public string Property10 { get; private set; }
- public int Property11 { get; private set; }
- public double Property12 { get; private set; }
- public MessageItemData( string property7, string property8, DateTime property9, DateTime property10, int property11, double property12)
- {
- this.Property7 = property7;
- this.Property8 = property8;
- this.Property9 = property9.ToShortDateString();
- this.Property10 = property10.ToShortDateString();
- this.Property11 = property11;
- this.Property12 = property12;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement