public MailMessage GenerateMessage(string key, IDictionary <string, object> parameters, bool useDynamicSource, string[] to, string[] cc, string[] bcc) { var template = GetMailTemplate(key); var source = new Dictionary <string, object>(); if (useDynamicSource) { if (template.DynamicSourcesDefinition.Count == 0) { throw new DynamicSourceDefinitionNotFoundException(); } template.DynamicSourcesDefinition.ToList() .ForEach(i => { var type = Type.GetType(i.TypeName); if (type == null) { throw new TypeNotFoundException(i.TypeName); } object instance = null; try { instance = ServiceLocator.Get(type); } catch { instance = Activator.CreateInstance(type); } if (instance == null) { throw new Exception("Not initialized instance. TypeName: " + i.TypeName); } var method = type.GetMethod(i.MethodName); var inputParameters = method.GetParameters(); if (inputParameters.Length == 0) { var methodResult = method.Invoke(instance, new object[] { }); template.StaticSources.Add(i.Key, methodResult); } else if (inputParameters.Length == 1 && inputParameters.First().ParameterType. Name.Contains("Request")) { var methodParameter = inputParameters.First(); var request = Activator.CreateInstance(methodParameter.ParameterType); var properties = methodParameter.ParameterType.GetProperties() .Where(p => parameters.ContainsKey(p.Name)) .ToList(); properties.ForEach(p => p.SetValue(request, parameters[p.Name], null)); var methodResult = method.Invoke(instance, new[] { request }); source.Add(i.Key, methodResult); } else { var methodResult = method.Invoke(instance, parameters.Values.ToArray()); source.Add(i.Key, methodResult); } }); } else { parameters.ToList().ForEach(item => source.Add(item.Key, item.Value)); } var subject = _templateEngine.Merge(template.Subject, source); var body = _templateEngine.Merge(template.Body, source); if (!string.IsNullOrEmpty(template.Signature)) { body += "<br/>" + _templateEngine.Merge(template.Signature, source); } if (string.IsNullOrEmpty(body)) { body = string.Format("TemplateKey:{0}", key); } if (string.IsNullOrEmpty(subject)) { subject = string.Format("TemplateKey:{0}", key); } var message = new MailMessage { Body = body, Subject = subject, IsBodyHtml = true, BodyEncoding = Encoding.UTF8, SubjectEncoding = Encoding.UTF8 }; if (to != null && to.Length > 0) { to.ToList().ForEach(message.To.Add); } if (cc != null && cc.Length > 0) { cc.ToList().ForEach(message.CC.Add); } if (bcc != null && bcc.Length > 0) { bcc.ToList().ForEach(message.Bcc.Add); } if (template.To.Count > 0) { template.To.ForEach(message.To.Add); } if (template.Cc.Count > 0) { template.Cc.ForEach(message.CC.Add); } if (template.Bcc.Count > 0) { template.Bcc.ForEach(message.Bcc.Add); } return(message); }