使用邮政MVC与布局解析标题作为邮件正文

时间:2014-04-18 13:07:31

标签: asp.net-mvc postal

似乎当我使用Postal使用Layout发送电子邮件时,标题未被解析并包含在邮件消息中。

查看/电子邮件/ _ViewStart.cshtml

@{ Layout = "~/Views/Emails/_EmailLayout.cshtml"; }

查看/电子邮件/ _EmailLayout.cshtml     

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ViewEmails</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

查看/电子邮件/ ResetPassword.cshtml

To:  @ViewBag.To
From: @ViewBag.From
Subject: Reset Password
Views: Html

查看/电子邮件/ ResetPassword.html.cshtml

Content-Type: text/html; charset=utf-8

Here is your link, etc ...

当我收到邮件时,所有标题To,From,Subject和Views都包含在正文中。

任何人都知道如何正确地做到这一点?

更新(感谢Andrew),这有效:

查看/电子邮件/ _EmailLayout.cshtml

@RenderSection("Headers", false)
<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>ViewEmails</title>
</head>
<body>
    <div>
        @RenderBody()
    </div>
</body>
</html>

查看/电子邮件/ ResetPassword.cshtml

@section Headers {
    To:  @ViewBag.To
    From: @ViewBag.From
    Subject: Reset Password
    Views: Html
}

查看/电子邮件/ ResetPassword.html.cshtml

@section Headers {
    Content-Type: text/html; charset=utf-8
}

Here is your link, etc ...

2 个答案:

答案 0 :(得分:16)

一种选择是使用Razor部分。

在布局的顶部添加:

@RenderSection("Headers")

然后在视图中添加:

@section Headers {
    To:  @ViewBag.To
    From: @ViewBag.From
    Subject: Reset Password
    Views: Html
}

答案 1 :(得分:0)

移动第一行

Content-Type:text / html;字符集= UTF-8

从Views / Emails / ResetPassword.html.cshtml到Views / Emails / _EmailLayout.cshtml

Content-Type: text/html; charset=utf-8
<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>ViewEmails</title>
    </head>
    <body>
        <div>
            @RenderBody()
        </div>
    </body>
</html>