using System; using System.Collections.Generic; using System.IO; using System.Net.Mail; using System.Reflection; using NVelocity; using NVelocity.App; using Spring.Objects.Factory; using log4net; using Spring.Template.Velocity; namespace Sleis.Utility { public class EmailUtility : IInitializingObject { private readonly ILog _log = LogManager.GetLogger(typeof(EmailUtility)); public SmtpClient Smtp { get; set; } public System.Net.ICredentialsByHost Credentials { get; set; } public Dictionary Messages { get; set; } public VelocityEngine VelocityEngine { get; set; } public void AfterPropertiesSet() { ArgumentValidationUtility.ThrowOnNull(Smtp, "Null Smtp"); ArgumentValidationUtility.ThrowOnNull(Messages, "Null Messages"); ArgumentValidationUtility.ThrowOnNull(VelocityEngine, "Null VelocityEngine"); if (!Smtp.UseDefaultCredentials) Smtp.Credentials = Credentials; } public void SendObject(string to, string messageKey, object instance) { _log.DebugFormat("Send({0},{1},{2})", to, messageKey, instance); Dictionary dict = new Dictionary(); dict.Add("item", instance); SendArgs(to, messageKey, dict, null); } public void SendArgs(string to, string messageKey, Dictionary args) { SendArgs(to, messageKey, args, null); } public void SendArgs(string to, string messageKey, Dictionary args, Attachment attachment) { _log.DebugFormat("Send({0},{1},{2})", to, messageKey, args); VelocityContext vcontext = new VelocityContext(); if (args != null) { foreach (KeyValuePair key in args) { vcontext.Put(key.Key, key.Value); } } string message = null; string template = String.Format("{0}.vm", messageKey); using (StringWriter writer = new StringWriter()) { VelocityEngine.MergeTemplate(template, "UTF-8", vcontext, writer); message = writer.ToString(); } Send(to, messageKey, message, attachment); } public void Send(string to, string messageKey, string body, Attachment attachment) { _log.DebugFormat("Send({0},{1},{2})", to, messageKey, body); if (String.IsNullOrEmpty(messageKey)) { throw new ArgumentNullException("messageKey"); } if (String.IsNullOrEmpty(to)) { throw new ArgumentNullException("to"); } if (String.IsNullOrEmpty(body)) { throw new ArgumentNullException("body"); } if (!Messages.ContainsKey(messageKey)) { throw new ArgumentOutOfRangeException("Invalid messageKey: " + messageKey); } MailMessage msg = Messages[messageKey]; try { msg.To.Clear(); msg.To.Add(to); msg.Attachments.Clear(); //since we are using spring to manage email messages as static object, we need to make sure we clear out all attachments. That way each email is in a way "fresh" msg.Body = body; if (attachment != null) { msg.Attachments.Add(attachment); } Smtp.Send(msg); } catch (Exception ex) { _log.Error("Error while sending message", ex); //TODO: Not sure if we want to swallow message or not. Originally we were swallowing, however, this created problem on FeedBack screen since it appeared message was sending successfully. //Throwing exception now might cause problems if not properly catching for them in UI. throw new ApplicationException("Errow while sending message: " + ExceptionUtility.GetDeepExceptionMessageOnly(ex)); } finally { //since we are using spring to manage email messages as static object, we need to make sure we clear out all attachments. That way each email is in a way "fresh" msg.Attachments.Clear(); } } } }