如何在ASP.NET中创建电子邮件模板 [英] How do I create a Email template in ASP.NET

查看:92
本文介绍了如何在ASP.NET中创建电子邮件模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个LMS,其中在各个阶段从系统生成大量电子邮件。目前,这是通过一个类来完成的,其中电子邮件的内容是在开发过程中预先定义的(这不是一个好的编码)。最终用户需要一个设施,通过该设施,他可以创建电子邮件模板(正文),并且应该提供常用参数(如学生姓名,课程等)以便将其包含在正文中,在运行模式下系统将选择此模板并发送邮件。

我在一些Web应用程序中看到了类似的功能。任何人都可以帮助我实现这个目标。在此先感谢。

解决方案

这是表单提供的工具。不是电子邮件模板!



您需要做的是创建一个简单的表单,其中包含一些基本字段,例如我是学生/老师,我想选择[课程]来自你的[学院]。这些是每封电子邮件中唯一不同的东西。所以你可以为它设计HTML。假设如下,



 <  表单   方法  =  发布 >  
<! - 基本字段 - >
<! - 应用程序名称 - >
名称< 输入 type = text 名称 = 名称 / >
< ;! - 他的类型,学生或老师 - - >
我是< select < span class =code-attribute> name = type >
< 选项 value = 学生 > 学生< / option >
< 选项 = 老师 > 教师< / option > ;
< / select >
<! - 课程代码 - >
课程代码< 选择 name = course >
< 选项 value = 845 > 845 < / option >
< 选项 value = 846 > 846 < / option >
< 选项 = 847 > 847 < / option >
< / select >
<! - 他将填写的申请 - >
申请< ; textarea name < span class =code-keyword> = 应用程序 > < / textarea >
<! - 输入按钮 - >
< 输入 类型 = 提交 value = 提交 < span class =code-attribute> / >
< / form >





..这就足够了。既然你说你正在使用ASP.NET(在标签中),我会使用C#代码提供你要编写的代码的剩余模板来启用这个功能。



  //  变量初始化 
var name = Request [ name ]。
var type =请求[ type ];
var course =请求[ course ];
var application = Request [ application ];

// 检查请求是否为POST
if (IsPost){
// 制定出来电子邮件
// 您可以添加自己的验证,例如
// 如果name不为null或为空字符串
if (name!= && name!= < span class =code-keyword> null
){
WebMail.Send(to: recipient_email@example.com
主题: 已收到应用程序
// 创建电子邮件正文。

body: 感谢您申请 +名称+
\ n您应用为 + type +
课程代码 + course +
,您的应用程序为\ n + application
);
}
}





一旦发送,它将包含来自用户的动态数据。您需要设计HTML以使用户能够从一些提供的工具和选项中进行选择。然后使用这些值并填充HTML电子邮件。



您可以从本文中了解有关使用ASP.NET发送电子邮件的更多信息。 轻松使用ASP.NET帮助程序发送电子邮件 [ ^ ]


I'm developing an LMS in which lot of emails are generated from the system at various stages. Currently this is done through a class in which the content of email is pre-defined during the development itself (which is not a good coding). The end user need a facility by which he can create Email templates (body) and common parameters (like student name, course etc) should be made available so that he will include it in the body, in the run mode the system will pick this template and send the mail.
I have seen similar facility in some of the web applications. Can any one help me on how to implement this. Thanks in advance.

解决方案

That is a facility that a form provides. Not an Email template!

What you need to do is to create a simple form that has some basic fields like I am a student/teacher, I would like to choose [course] from your [institute]. These are the only things that will differ in each of the email. So you can design the HTML for that. Suppose the following,

<form method="post">
   <!-- Basic fields -->
   <!-- Name of the application -->
   Name <input type="text" name="name" />
   <!--  His type, student or a teacher-->
   I am a <select name="type">
             <option value="student">Student</option>
             <option value="teacher">Teacher</option>
          </select>
   <!-- Course code -->
   Course code <select name="course">
                  <option value="845">845</option>
                  <option value="846">846</option>
                  <option value="847">847</option>
               </select>
   <!-- Application that he will fill out -->
   Application <textarea name="application"></textarea>
   <!-- Input button -->
   <input type="submit" value="Submit" />
</form>



..this is enough. Now since you said that you're using ASP.NET (in the tags) I would use the C# code to provide the remaining template of the code that you will write to enable this feature.

// variable initialization
var name = Request["name"];
var type = Request["type"];
var course = Request["course"];
var application = Request["application"];

// checking if the request was POST
if(IsPost) {
   // working out the EMAIL
   // you can add your own validations such as 
   // if name is not null or empty string
   if(name != "" && name != null) {
      WebMail.Send(to: "recipient_email@example.com",
                   subject: "Application recieved", 
                   // create the body of the email.
                   body: "Thanks for applying " + name + 
                         "\nYou applied as a " + type + 
                         " for the course code " + course +
                         " and your application was as \n" + application
                  );
   }
}



Once this is sent, it will contain the dynamic data from the user. You will need to design your HTML as to enable the user to select from some provided tools and options. Then use those values and populate the HTML email.

You can learn more on sending emails using ASP.NET, from this article. Sending Emails Easily Using ASP.NET Helpers[^]


这篇关于如何在ASP.NET中创建电子邮件模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆