/// <summary> /// Loads email message template from a file and performs transformation. /// </summary> /// <param name="options"> /// Program options. /// </param> /// <param name="data"> /// Dictionary object holding substitution strings used by the transformation. /// </param> /// <returns> /// Mail message generated from the template file. /// </returns> private static MailMessage ProcessTemplate ( Options options, Dictionary <string, object> data ) { // The Mailr class used for template transformations. MailTemplate msg = new MailTemplate(); // If the template file name format was not specified, // use the default format. if (!String.IsNullOrEmpty(options.TemplateNameFormat)) { msg.FileNameFormat = options.TemplateNameFormat; } // If the mail subject was specified, set it explicitly. // Doing so would ignore the contents of the <title> tag. if (!String.IsNullOrEmpty(options.MailSubject)) { msg.Subject = options.MailSubject; } // Either load template form the specified file, // or use the template parts to generate the file name // on the fly. try { if (String.IsNullOrEmpty(options.TemplateFilePath)) { msg.Load ( options.TemplateDirPath, // required options.TemplateName, // required options.TemplateCulture, // optional options.TemplateExtension // optional ); } else { msg.Load(options.TemplateFilePath); } } catch (Exception ex) { String errMsg; if (String.IsNullOrEmpty(options.TemplateFilePath)) { errMsg = String.Format("Cannot load template from directory '{0}'.", options.TemplateDirPath); } else { errMsg = String.Format("Cannot load template from file '{0}'.", options.TemplateFilePath); } throw new Exception(errMsg, ex); } // If we have data, perform substitutions in the template. try { if (data == null) { msg.Transform(); } else { msg.Transform(data); } } catch (Exception ex) { throw new Exception("Cannot transform template.", ex); } return(msg as MailMessage); }