通过呈现ASP .NET MVC 3页面发送电子邮件

时间:2011-12-03 15:42:03

标签: c# asp.net-mvc asp.net-mvc-3

我希望通过将代码转换为MVC 3,使用MVC 2的旧方法发送电子邮件。 但它在这里给出了一个错误:

            Control control1 = vp1.LoadControl("/Views/Home/_SupportEmailAdmin.cshtml");
            vp1.Controls.Add(control);

错误消息:

类型'ASP._Page_Views_Home__SupportEmail_cshtml'不会从'System.Web.UI.UserControl'继承。

有没有正确的方法呢?

以下是代码:

 ViewBag.SupportRequest = this.SupportRequest;

                    ViewPage vp1 = new ViewPage();
                    vp1.ViewData = ViewData;

                    Control control1 = vp1.LoadControl("/Views/Home/_SupportEmailAdmin.cshtml");
                    vp1.Controls.Add(control);

                    StringBuilder sb1 = new StringBuilder();
                    using (StringWriter sw1 = new StringWriter(sb1))
                    {
                        using (HtmlTextWriter tw1 = new HtmlTextWriter(sw1))
                        {
                            vp1.RenderControl(tw1);
                        }
                    }

                    sb1.Insert(0, @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd""><html xmlns=""http://www.w3.org/1999/xhtml""><body>");
                    sb1.Append("</body></html>");

                    MailMessage msg1 = new MailMessage(new MailAddress(this.SupportRequest.EMail, this.SupportRequest.Name),
                        new MailAddress(from, fromName));
                    msg1.Subject = fromSubject;
                    msg1.IsBodyHtml = true;
                    msg1.Body = sb1.ToString();
                    ....

用两个词来说,问题是如何使用MVC 3页面模板将其呈现为文本并发送。

更新

最终解决方案......

#region Send Support Request to Admin

                StreamReader streamReader1 = new StreamReader(Server.MapPath("/Views/Home/_SupportEmailAdmin.cshtml"));
                string emailTemplate1 = streamReader1.ReadToEnd();
                streamReader1.Close();

                string result1 = Razor.Parse(emailTemplate1, new { Name = sr.Name, EMail = sr.EMail, Subject = sr.Subject, Message = sr.Message });

                StringBuilder sb1 = new StringBuilder();
                sb1.Insert(0, @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Strict//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd""><html xmlns=""http://www.w3.org/1999/xhtml""><body>");
                sb1.Append(result);
                sb1.Append("</body></html>");

                MailMessage msg1 = new MailMessage(new MailAddress(sr.EMail, sr.Name),
                    new MailAddress(from, fromName));
                msg1.Subject = fromSubject;
                msg1.IsBodyHtml = true;
                msg1.Body = sb1.ToString();

                #endregion

                smtp.Send(msg1);

和cshtml的内容

file

<table>
    <tbody>
        <tr>
            <td>
                Name
            </td>
            <td>@Model.Name
            </td>
        </tr>
        <tr>
            <td>
                Email
            </td>
            <td>@Model.EMail
            </td>
        </tr>
        <tr>
            <td>
                Subject
            </td>
            <td>@Model.Subject
            </td>
        </tr>
        <tr>
            <td>
                Message
            </td>
            <td>@Model.Message
            </td>
        </tr>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:1)

看一下RazorEngine项目

http://razorengine.codeplex.com/

并且可能准备好使用解决方案 - MvcMailer

https://github.com/smsohan/MvcMailer/wiki/MvcMailer-Step-by-Step-Guide