ASP.NET MVC:如何使用控制器发送html电子邮件? [英] ASP.NET MVC: How to send an html email using a controller?
问题描述
使用asp.net发送定制的html电子邮件的最简单方法是什么?
我想,最好我想通过电子邮件发送html,而不是返回通过ActionResult到浏览器,正如我通常所说。这样,我可以构建一个视图的电子邮件,通过一个模型提供数据,然后使用标准的.NET电子邮件启动它。
这是可行/谢谢,
http://www.brightmix.com/blog/renderpartial-to- string-in-asp-net-mvc / 有一个很好的解决方案,用于将视图呈现到字符串,以便您可以通过电子邮件发送。
///静态方法来渲染字符串 - 放在你选择的地方
public static string RenderPartialToString(string controlName,object viewData)
{
ViewDataDictionary vd = new ViewDataDictionary (可视数据);
ViewPage vp = new ViewPage {ViewData = vd};
控制控件= 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();
}
What would be the simplest way to send a customised html email using asp.net?
I suppose ideally I would like to send html via email rather than returning it to the browser via a ActionResult, as I normally would. This way I could build the email as a view, supply it with data via a model and then fire it using standard .NET email stuff.
It this feasible / the way to do it?
Thanks,
http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/ has a good solution for rendering a View to a string so you can send it in email.
/// 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();
}
这篇关于ASP.NET MVC:如何使用控制器发送html电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!