0

我正在使用 razorengine 发送电子邮件。senden不是问题,只是内容在内容中只显示为/da/Property/Property/Mail/TellAFriendTextMail。

private void SendTellAFriendMail(NotificationTellAFriend mailData)
    {

      string textmail = this.GetMailAsText(mailData);
      string htmlmail = this.GetMailAsHTML(mailData);

      MailAddress from = new MailAddress("robot@mail.com", mailData.SenderName);
      MailAddress to = new MailAddress(mailData.receiverMail, mailData.SenderName);

      using (AlternateView htmlview = this.CreateView(htmlmail, "text/html"))
      using (AlternateView textview = this.CreateView(textmail, "text/plain"))
      using (MailMessage email = new MailMessage(from, to))
      {
        email.Subject = string.Format(CultureInfo.InvariantCulture, Resources.Resources._MailSubject, mailData.SenderName);
        email.AlternateViews.Add(textview);
        email.AlternateViews.Add(htmlview);
        email.ReplyToList.Add(new MailAddress(mailData.receiverMail, mailData.SenderName));

        using (SmtpClient client = new SmtpClient())
        {
          client.Send(email);
        }

      }
    }

 private string GetMailAsHTML(NotificationTellAFriend mailData)
    {
      return Razor.Parse(Url.Action("/Property/Mail/TellAFriendTextMail"), mailData);
    }
4

1 回答 1

1

Razor.Parse方法的第一个参数不是 url,而是您要解析的实际 Razor 内容。所以:

private string GetMailAsHTML(NotificationTellAFriend mailData)
{
    var razorTemplateFile = Server.MapPath("~/Property/Mail/TellAFriendTextMail.cshtml");
    var razorTemplate = File.ReadAllText(razorTemplateFile);
    return Razor.Parse(razorTemplate, mailData);
}
于 2013-01-14T06:54:36.603 回答