Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 在何处存储电子邮件模板_.net_Asp.net_Email Templates - Fatal编程技术网

.net 在何处存储电子邮件模板

.net 在何处存储电子邮件模板,.net,asp.net,email-templates,.net,Asp.net,Email Templates,我有一个asp.net web应用程序,它在注册过程中向用户发送几封电子邮件。现在我将它们与代码内联,但我希望将它们放在一个中心位置,在那里我可以编辑它们,而无需进入VS 存储这些HTML模板的最佳位置/格式是什么?这取决于模板的更改频率以及更改者。例如: 由应用程序的用户更改,更改是紧急且可能频繁的: 最好存储在数据库中,并在每次发送电子邮件时加载 由开发人员(即您)更改,更改很少且不紧迫: Web服务器上的文本文件,将它们加载到缓存中并存储在那里,只有在缓存删除或应用程序重新启动时才重

我有一个asp.net web应用程序,它在注册过程中向用户发送几封电子邮件。现在我将它们与代码内联,但我希望将它们放在一个中心位置,在那里我可以编辑它们,而无需进入VS


存储这些HTML模板的最佳位置/格式是什么?

这取决于模板的更改频率以及更改者。例如:

由应用程序的用户更改,更改是紧急且可能频繁的:

  • 最好存储在数据库中,并在每次发送电子邮件时加载
由开发人员(即您)更改,更改很少且不紧迫:

  • Web服务器上的文本文件,将它们加载到缓存中并存储在那里,只有在缓存删除或应用程序重新启动时才重新加载它们

您可以将电子邮件模板存储在
.html文件中。然后将其格式化为支持您要包含的参数的格式。e、 g

<head>
<title></title>
</head>
<body>
    Hello <!--Name--> ,
    This is a test template 
    User Name: <!--UserName-->
    .............................
    .............................
</body>
</html>

你好
这是一个测试模板
用户名:
.............................
.............................

无论何时向用户发送电子邮件,都要将模板设置为特定于用户,因此,您可以在运行时替换该参数。

我建议将电子邮件模板存储在XML文件中,这样可以通过向邮件模板添加属性来实现将来的可扩展性,并且还可以方便地进行编辑。

我将我的web应用程序的所有电子邮件模板存储为ASP.NET MVC Razor View,但是作为嵌入式资源,我可以很容易地从任何项目中引用它

模板如下所示(注意本地化):

要实际发送电子邮件,我使用以下命令进行渲染:

public void SendSystemEmail<T>(string templateName, string subject, string fromName, string recipientEmail, T model)
{
    dynamic template = this.GetEmailTemplate(templateName);
    string layoutBody = RazorEngine.Razor.Parse(template.Layout as string, model);
    string emailBody = RazorEngine.Razor.Parse(template.Template as string, model);

    emailBody = layoutBody.Replace(CONTENTSECTIONREPLACETOKEN, emailBody);

    PreMailer.Net.PreMailer pm = new PreMailer.Net.PreMailer();
    emailBody = pm.MoveCssInline(emailBody, true);

    EmailDto email = new EmailDto();
    email.Body = emailBody;
    email.IsBodyHtml = true;
    email.FromEmail = "support@example.com";
    email.ReplyToEmail = email.FromEmail;
    email.FromName = fromName;
    email.RecipientEmail = recipientEmail;
    email.Subject = subject;
    email.Type = EmailTypes.Transactional;

    if (String.IsNullOrWhiteSpace(email.FromName))
    {
        email.FromName = "Milkshake Software";
    }

    this.SendMailMessages(new List<EmailDto>() { email }, false);
}

感谢大家对他们的处理方式提出见解。我从这里收集了很多知识。我喜欢@MartinHN使用Razor解析器和具体的数据模型

然而,有些事情对我来说并不太好

要求:

  • 我必须存储电子邮件模板,以便将其显示给
    利益相关者随时都可以。因此,它应该可以浏览 内部网-最好通过sae网站作为托管API

  • 前端设计师应该能够轻松地修改模板。 因此,我想以简单的HTML格式存储它,这样设计器就可以了 不必过多地讨论技术细节

  • 电子邮件模板应易于修改,以便 管理员(未来要求)。在不久的将来,将有 不同的短信通知,屏幕。因此,模板是 不一样

基于这些要求,我做了以下工作:

  • 因为我使用的是MVC,所以我创建了一个名为“STATIC”的文件夹 可直接浏览(和MVC引擎/http处理程序 将此文件夹从其MVC活动中排除)

    使用这种方法,我可以轻松地实现第一个需求,并且 可以将我的链接发送给股东作为

  • 每个电子邮件模板都提供了自己的html,因此对用户来说很容易 设计器访问该文件,并在其存储库中将其作为 “我的存储库”文件夹的快捷方式。Html有内联CSS,它是 完全独立的html-每封电子邮件

  • 最后一个主要需求是维护这些设计,用户可以修改这些设计。那么,我绝对不想通过文件系统来处理。我现在做的是将这些通知存储到数据库中,并对它们进行一次初始化。之后,管理面板有一个wysiwyg html编辑器,可以给他们一个快速预览,以及控制它应该发送到什么

  • 现在我想确保未来的需求能够轻松处理,因为我的公司正在为不同的模式引入不同的通知,如电子邮件、屏幕、短信通知。我决定借助存储这些答案的模板初始化器XML来扩展软件设计

    名为-MessageTemplates.xml的所有模板之母存储了初始化不同类型模板(即电子邮件、短信、屏幕等)所需的不同信息

    下面是代码现在的样子

     [HttpGet]
            [Route("applications/initializenotificationtemplate")]
            public IHttpActionResult InitializeNotificationTemplate()
            {
                return
                    InitializeNotificationTemplate(Path.Combine(HostingEnvironment.ApplicationPhysicalPath,
                        @"Static\InitializeData\MessageTemplates.xml"));
            }
    
    
    
    
    [NonAction]
            public IHttpActionResult InitializeMailTemplate(string filePath)
            {
                try
                {
                    _applicationService.InitializeTemplate(filePath);
                    return Ok("Application Notification templates are initialized.");
                }
                catch (Exception ex)
                {
                    return InternalServerError(ex);
                }
            }
    
    _applicationService.InitializeTemplate具有以下定义:

    public bool InitializeTemplate(string filePath)
            {
                if (string.IsNullOrEmpty(filePath))
                {
                    throw new ArgumentNullException("File Path");
                }
    
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException(filePath);
                }
    
    
                var data = _notificationTemplateService.Get();
                var exceptionMessages = string.Empty;
    
                if (data != null)
                {
                    var historicalTemplates = data.ToList();
                    historicalTemplates.ForEach((d) => _notificationTemplateService.Delete(d, out exceptionMessages));
                }
    
                XDocument xmlDocument = XDocument.Load(filePath);
                IEnumerable<NotificationTemplate> templates = (from template in xmlDocument.Descendants("Template")
                                                               select new NotificationTemplate()
                                                               {
                                                                   Title = template.Element("Subject").Value,
                                                                   Description = template.Element("Body").Value,
                                                                   Type = (NotificationTypeOptions)Enum.Parse(typeof(NotificationTypeOptions), template.Element("Type").Value, true),
                                                                   Category = (NotificationCategoryOptions)Enum.Parse(typeof(NotificationCategoryOptions), template.Attribute("category").Value, true),
                                                               }).ToList();
    
                foreach (var t in templates)
                {
                    var path = Path.Combine(Path.GetDirectoryName(filePath), Regex.Replace(t.Description, @"\t|\n|\r| ", ""));
                    if (File.Exists(path))
                    {
                        StreamReader reader = new StreamReader(path);
                        t.Description = reader.ReadToEnd();
                    }
                    else
                    {
                        t.Description = string.Empty;
                    }
                }
    
                return _notificationTemplateService.InsertRange(templates, out exceptionMessages);
            }
    
    这是我第一次在安装应用程序时调用initialize API调用,将通知模板安装到数据库中,并且所有其他选项都可用,可以随时使用


    现在,通过这种方法,我让组织中的每个人都感到高兴,并且它有很大的力量进一步扩展这种方法,这样我就可以很容易地引入新的模板。

    希望你被分类,这是我发现的,我认为更容易

  • 打开项目的属性
  • 去资源中心。在资源“添加资源”下=>“添加现有文件”
  • 然后你可以像这样访问它

    var html = Resources.YourFileName;
    
    记住使用

    using YourProject.Properties;
    

    我在我的网站中使用了xml文件,它使我能够极大地扩展电子邮件模板支持的功能。
    public bool InitializeTemplate(string filePath)
            {
                if (string.IsNullOrEmpty(filePath))
                {
                    throw new ArgumentNullException("File Path");
                }
    
                if (!File.Exists(filePath))
                {
                    throw new FileNotFoundException(filePath);
                }
    
    
                var data = _notificationTemplateService.Get();
                var exceptionMessages = string.Empty;
    
                if (data != null)
                {
                    var historicalTemplates = data.ToList();
                    historicalTemplates.ForEach((d) => _notificationTemplateService.Delete(d, out exceptionMessages));
                }
    
                XDocument xmlDocument = XDocument.Load(filePath);
                IEnumerable<NotificationTemplate> templates = (from template in xmlDocument.Descendants("Template")
                                                               select new NotificationTemplate()
                                                               {
                                                                   Title = template.Element("Subject").Value,
                                                                   Description = template.Element("Body").Value,
                                                                   Type = (NotificationTypeOptions)Enum.Parse(typeof(NotificationTypeOptions), template.Element("Type").Value, true),
                                                                   Category = (NotificationCategoryOptions)Enum.Parse(typeof(NotificationCategoryOptions), template.Attribute("category").Value, true),
                                                               }).ToList();
    
                foreach (var t in templates)
                {
                    var path = Path.Combine(Path.GetDirectoryName(filePath), Regex.Replace(t.Description, @"\t|\n|\r| ", ""));
                    if (File.Exists(path))
                    {
                        StreamReader reader = new StreamReader(path);
                        t.Description = reader.ReadToEnd();
                    }
                    else
                    {
                        t.Description = string.Empty;
                    }
                }
    
                return _notificationTemplateService.InsertRange(templates, out exceptionMessages);
            }
    
     public class NotificationTemplate  : IdentityBase
        {
            public string Category { get; set; }
            public NotificationTypeOptions Type { get; set; }
            public string Title { get; set; }
            public string Description { get; set; }
    
            public NotificationTemplate()
            {
                Type = NotificationTypeOptions.Email;
            }
        }
    
     [Flags]
        public enum NotificationTypeOptions
        {
            Email = 0,
            Screen = 1,
        }
    
    var html = Resources.YourFileName;
    
    using YourProject.Properties;