C# 修改部分视图中作为HTML电子邮件发送的数据
我正在尝试修改HTML电子邮件中发送的一些数据 这是我的控制器:C# 修改部分视图中作为HTML电子邮件发送的数据,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我正在尝试修改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
[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,我如何修复它?@Ben Robinson没有给出答案,但解决方案是在消息上使用
@HTML.Raw
。我想你应该这样做@HTML.Raw(msg)
,否则它将在msg
中转义任何HTML。让我检查一下。但我认为你是对的。作为回答,发帖,please@BenRobinson,请作为答案发布,以便我可以接受。@Ben Robinson,如果你想要分数,请作为答案发布!!!
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