我正在使用.NET MVC,目前我正在开发一个向特定用户发送电子邮件的组件。我创建了电子邮件功能,我可以发送电子邮件。但是现在,我正在考虑使用模板来改善它。
我的想法 -
将有一个文件(xml或文本或任何内容)包含电子邮件结构的特定模板,如下所示 -
主题:欢迎用户"用户(通过数据库)"
亲爱的" UserName(从数据库传递)",
欢迎使用该系统 - 更多行----
- 更多行----此致
管理
如何使用外部文件实现此目的。假设配置文件或XML或文本文件。任何概念都是受欢迎的。
答案 0 :(得分:0)
您可以使用Razor(* .cshtml)模板。
渲染
public static string RenderPartialView(this Controller controller, string viewName, object model)
{
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
用法
EMail.SendEmail(
from,
to,
sunj,
this.RenderPartialView("MT_RestorePassword"));
模板" Views \ Shared \ MT_RestorePassword.cshtml"
@{
ViewBag.Title = S.RestoringPassword;
}
<h2>@System.Configuration.ConfigurationManager.AppSettings["ASE.SiteName"] -
@S.RestoringPassword</h2>
<hr />
<h3><b>@S.User: </b> @ViewBag.User</h3>
<h3><b>@S.RestorePasswordCode: </b> @ViewBag.Code</h3>
答案 1 :(得分:0)
假设你有HTML模板
<strong>User Name: </strong> {UserName}<br />
<strong>Email Id: </strong> {UserEmail}<br />
现在在控制器上你可以使用它,并将静态值替换为从前端或任何源传递的静态值
public JsonResult FeedbackReply(MessageViewModel model)
{
string emailBody = System.IO.File.ReadAllText(HttpContext.Server.MapPath("~/Common/EmailTemplate/EmailTemplate.html"));
emailBody = emailBody.Replace("{EmailId}", model.UserEmailId);
emailBody = emailBody.Replace("{UserName}", model.UserName);
SendEmail.SendMailMessage(EmailSubjects.EmailReply,
emailBody, model.UserEmailId,
ConfigurationManager.AppSettings["CC"],
ConfigurationManager.AppSettings["CCName"]);
return Json(new { status = true, message = CustomMessages.EmailSentSuccessfully }, JsonRequestBehavior.AllowGet);
}
这是sendEmail类
public class SendEmail
{
/// <summary>
/// Send email
/// </summary>
/// <param name="subject"></param>
/// <param name="body"></param>
/// <param name="to"></param>
/// <param name="cc"></param>
/// <param name="ccName"></param>
/// <returns></returns>
public static bool SendMailMessage(string subject, string body, string to, string cc, string ccName)
{
using (MailMessage objMailMessage = new MailMessage())
{
using (SmtpClient objSmptp = new SmtpClient())
{
try
{
objMailMessage.To.Add(new MailAddress(to));
objMailMessage.Subject = subject;
objMailMessage.Body = body;
objMailMessage.IsBodyHtml = true;
objMailMessage.From = new System.Net.Mail.MailAddress(ConfigurationManager.AppSettings["MailFrom"], ConfigurationManager.AppSettings["MailName"]);
objMailMessage.Priority = MailPriority.High;
if (!string.IsNullOrWhiteSpace(cc) && !string.IsNullOrWhiteSpace(ccName))
{
MailAddress objMailAddress = new MailAddress(cc, ccName);
objMailMessage.CC.Add(objMailAddress);
}
System.Net.NetworkCredential basicAuthenticationInfo = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTPUserName"], ConfigurationManager.AppSettings["SMTPPassword"]);
objSmptp.Host = ConfigurationManager.AppSettings["SMTPHostName"];
objSmptp.Port = int.Parse(ConfigurationManager.AppSettings["Port"]);
objSmptp.UseDefaultCredentials = false;
objSmptp.Credentials = basicAuthenticationInfo;
objSmptp.EnableSsl = true;
objSmptp.Send(objMailMessage);
return true;
}
catch (Exception ex)
{
var exception = ex;
return false;
}
}
}
}
}