Exemplo n.º 1
0
        /// <summary>
        /// 根据模板文件路径,构建邮件模板实体
        /// </summary>
        /// <param name="templateFileName"></param>
        /// <returns></returns>
        private static MailTemplate BuildMailTemplate(string templateFileName)
        {
            string getDefaultTemplatesFolder = Directory.GetParent(MAIL_TEMPLATES_FILES_CONFIG_PATH).FullName;
            string getSingleTemplteFullPath  = string.Empty;

            if (!Path.IsPathRooted(templateFileName))
            {
                //如果是相对路径:
                getSingleTemplteFullPath = Path.Combine(getDefaultTemplatesFolder, templateFileName);
            }

            if (!File.Exists(getSingleTemplteFullPath))
            {
                throw new ApplicationException(string.Format("未找到模板配置文件 :{0} !", templateFileName));
            }
            MailTemplate returnTemplate = new MailTemplate();
            XElement     doc            = XElement.Load(getSingleTemplteFullPath);

            if (null != doc)
            {
                int mailPriority = 0;
                int.TryParse(doc.Element("mailPriority").Value, out mailPriority);

                returnTemplate.Sender            = doc.Element("sender").Value;
                returnTemplate.SenderDisplayName = (doc.Element("senderDisplayName") == null ? string.Empty : doc.Element("senderDisplayName").Value);
                returnTemplate.To           = (doc.Element("to") == null ? string.Empty : doc.Element("to").Value);
                returnTemplate.TemplateID   = doc.Element("id").Value;
                returnTemplate.IsHtmlType   = doc.Element("isHtmlType").Value == "1" ? true : false;
                returnTemplate.MailPriority = mailPriority;
                returnTemplate.Subject      = doc.Element("subject").Value.Replace("\n", string.Empty).Trim();
                //returnTemplate.Body = doc.Element("body").Value;
                List <KeyValuePair <string, string> > templateList =
                    doc.Element("body").Elements("template").Select(x => new KeyValuePair <string, string>(x.Attribute("reference") == null ? null : x.Attribute("reference").Value, x.Value)).ToList();
                if (templateList != null && templateList.Count > 0)
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var entry in templateList)
                    {
                        if (string.IsNullOrWhiteSpace(entry.Key))
                        {
                            sb.Append(entry.Value);
                        }
                        else
                        {
                            string path = entry.Key;
                            if (!Path.IsPathRooted(path))
                            {
                                path = Path.Combine(Path.GetDirectoryName(getSingleTemplteFullPath), path);
                            }
                            if (File.Exists(path))
                            {
                                sb.Append(File.ReadAllText(path));
                            }
                        }
                    }
                    returnTemplate.Body = sb.ToString();
                }
                else
                {
                    returnTemplate.Body = string.Empty;
                }
            }
            return(returnTemplate);
        }