通过渲染asp.net MVC 3的页面发送电子邮件

本文关键字:电子邮件 MVC asp net | 更新日期: 2023-09-27 18:16:46

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

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

错误信息:

类型的ASP。

有正确的方法吗?

代码如下:

 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>

通过渲染asp.net MVC 3的页面发送电子邮件

看看RazorEngine项目

http://razorengine.codeplex.com/

和可能准备使用解决方案- MvcMailer

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