修改以HTML电子邮件形式发送的部分视图中的数据

本文关键字:视图 数据 HTML 电子邮件 修改 | 更新日期: 2023-09-27 18:28:22

我正在尝试修改HTML电子邮件中发送的一些数据。

这是我的控制器:

[HttpPost, ValidateInput(false)]
public ActionResult ContactUs(ContactViewModel formdata)
{
    // Now send the email
    bool process = false;
    // Check to see if this is a bot, The field SHOULD NOT be filled in if it is not a bot. However, 
    // if it is a human, there SHOULD be a Phone number entered.
    process = (formdata.CommentSlider == 45);
    if (process)
    {
        MailMessage message = new MailMessage();
        message.From = new MailAddress("myaddress@myemail.com", "Our Company");
        switch (formdata.Subject)
        {
            case "Website Contact - General Questions":
            case "Website Contact - Review Requests":
                MailAddress to = new MailAddress("myaddress@myemail.com");
                MailAddress bcc1 = new MailAddress("myotheraddress@scic.com");
                message.To.Add(to);
                message.Bcc.Add(bcc1);
                break;
            case "Website Contact - Website Technical Issues":
                MailAddress to2 = new MailAddress("myaddress@myemail.com");
                MailAddress cc = new MailAddress("myotheraddress@scic.com");
                message.To.Add(to2);
                message.CC.Add(cc);
                break;
        }
        message.IsBodyHtml = true;
        message.Subject = formdata.Subject.Replace("Website Contact", "MySite Contact");
        formdata.Description1 = String.Concat("UserName: ", formdata.Name);
        formdata.Description2 = String.Concat("PID: ", formdata.PID);
        message.Body = Utilities.RenderPartialViewToString(this, "_ContactEmailForm", formdata);
        SmtpClient client = new SmtpClient();
        client.Send(message);
        // Now send client a copy if requested.
        // Doing it here because we don't want to just CC the participant.
        if (formdata.CopyMe)
        {
            message.To.Clear();
            message.Bcc.Clear();
            message.CC.Clear();
            message.To.Add(new MailAddress(formdata.thisisit));
            message.IsBodyHtml = true;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.Subject = formdata.Subject;
            formdata.Description1 = "MySite Contact Form Submission";
            formdata.Description2 = "";
            message.Body = Utilities.RenderPartialViewToString(this, "_ContactEmailForm", formdata);
            client.Send(message);
        }
    }
    if (formdata.referrer != null)
    {
        string url = formdata.referrer;
        return Redirect(url);
    }
    else
    {
        return RedirectToAction("LogOn", "Account");
    }
}

以下是RenderPartialViewToString()函数,它获取部分视图并将其返回到电子邮件正文中:

public static string RenderPartialViewToString(Controller controller, string viewName, object model)
{
    controller.ViewData.Model = model;
    try
    {
        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
            viewResult.View.Render(viewContext, sw);
            return sw.GetStringBuilder().ToString();
        }
    }
    catch (Exception ex)
    {
        return ex.ToString();
    }
}

这是部分视图:

@model MySite.Models.ContactViewModel
@{
    ViewBag.Title = "MySite Contact Form Submission";
    string Description = "";
    if(Model.Description2 !=  "") 
    {
        Description = String.Concat(Model.Description1, "<br />", Model.Description2);
    } 
    else 
    {
        Description = Model.Description1;    
    }
}
<h2>@Html.Raw(Description)</h2>
<p>
    The following information was submitted through the MySite contact page.
</p>
<p>
    Subject: @Model.Subject<br />
    Name: @Model.Name<br />
    Phone #: @Model.Phone<br />
    Email: @Model.thisisit<br />
    Member#: @Model.PID<br />
    @{
        string msg = Model.Message;
        if (Model.Subject == "Website Contact - Review Requests")
        {
            msg = msg.Replace("CName:", "<br />CName:").Replace("CDate:", "<br />CDate:").Replace("Mailing Address:", "<br />Mailing Address:");
        }
    }
    Message: @msg<br />
</p>

问题是消息中的"
"没有显示为HTML,下面是一个示例:

Please provide the information below. <br />CName: Another Test <br />CDate: Another Test <br />Mailing Address: Another Test

它应该是这样的:

Please provide the information below. 
CName: Another Test
CDate: Another Test 
Mailing Address: Another Test

是什么导致它不是HTML?我该如何修复它?

修改以HTML电子邮件形式发送的部分视图中的数据

@Ben Robinson没有作为答案发布,但解决方案是在消息上使用@Html.Raw