有一个构建操作 - 内容不会将文件嵌入到 .dll 中,它被部署到您的程序集目录(通常是 \bin)中,然后在您拥有模板的相同文件夹结构中。写起来很混乱,所以在这里是一个例子:
Project Directory
-Templates
-EmailTemplateA
编译和部署后,EmailTemplateA 将位于以下位置: \bin\Templates\EmailTemplateA
现在我们知道它在哪里,我们需要使用它。下面是一个代码片段,它会加载一个模板,替换一些值,然后发送您的电子邮件
public void SendRegistrationConfirmation(string toAddress, string confirmUrl)
{
const string subject = "Your Registration";
//load the template
var template = File.OpenText(AssemblyDirectory + " \\Templates\\NewProgramRegistration.Template").ReadToEnd();
//replace content in the template
//We have this #URL# string in the places we want to actually put the URL
var emailContent = template.Replace("#URL#", confirmUrl);
//Just a helper that actually sends the email, configures the server, etc
this.SendEmail(toAddress, subject, emailContent);
}