Ser*_*gio 7 html asp.net email asp.net-mvc
使用asp.net发送自定义html电子邮件的最简单方法是什么?
我想理想情况下我想通过电子邮件发送html而不是像往常一样通过ActionResult将其返回到浏览器.通过这种方式,我可以将电子邮件构建为视图,通过模型为其提供数据,然后使用标准的.NET电子邮件内容将其激活.
这可行/这样做的方法?
谢谢,
此博客文章提供了一个很好的解决方案,可以将View呈现为字符串,以便您可以通过电子邮件发送它.
/// Static Method to render string - put somewhere of your choosing
public static string RenderPartialToString(string controlName, object viewData)
{
ViewDataDictionary vd = new ViewDataDictionary(viewData);
ViewPage vp = new ViewPage { ViewData = vd };
Control control = vp.LoadControl(controlName);
vp.Controls.Add(control);
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter tw = new HtmlTextWriter(sw))
{
vp.RenderControl(tw);
}
}
return sb.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我认为在mvc中发送电子邮件与在web表单中发送电子邮件是一样的,您只需要将邮件消息的属性设置为启用html然后就可以了.喜欢这段代码
MailMessage mm = new MailMessage(emmailFrom,emailTo);
mm.Subject = "Your Subject";
mm.IsBodyHtml = true;
mm.Body = body.ToString();
SmtpClient smtp = new SmtpClient();
smtp.Send(mm);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12374 次 |
最近记录: |