此电子邮件已设置 - 正确构建和发送包含所有字段的数据。
但是,一旦我向方法添加条件,尽管单步执行代码并看到我的字符串构建正确,但只有第一个字段被发送。
这似乎是某种范围界定问题,我很确定当 _bodyBuilder 添加到 message.Body 时会发生问题,但我并不肯定。
可能在这条线附近的某个地方: message.Body = _bodyBuilder.ToString(); message.IsBodyHtml = true;
问题是我无法在本地完全测试这个过程。 SMTP 未设置为在本地使用。我每次都必须进行更新,然后将 dll 推送到服务器以进行测试。
如果需要,很乐意提供更多信息。谢谢!
// Small portion of the partial view
<section>
<header>
<h2>Report Name</h2>
</header>
<div class="editor-field">
@Html.TextBoxFor(model => model.ReportName)
</div>
</section>
<section>
<header>
<h2>@Html.LabelFor(model => model.Alert)</h2>
<span>(e.g. "Be aware of closure on X trail")</span>
</header>
<div class="editor-field summary">
@Html.Hidden("alertHidden", "")
@Html.TextAreaFor(model => model.Alert, new { @class = "alertForHidden" })
</div>
</section>
<section>
<header>
<h2>Alert Details</h2>
<span>(Use this field to include a longer narrative explaining in further detail the alert information provided in the Alert field.)</span>
</header>
<div class="editor-field summary">
@Html.Hidden("noticesHidden", "")
@Html.TextAreaFor(model => model.Notices, new { @class="noticesForHidden"})
</div>
</section>
// Small portion of the Javascript
$(".alertForHidden").change(function () {
console.log($("#alertHidden").val());
$("#alertHidden").val("true");
console.log($("#alertHidden").val());
});
$(".noticesForHidden").change(function () {
$("#noticesHidden").val("true");
});
$(".statusForHidden").click(function () {
$("#statusHidden").val("true");
});
$(".conditionForHidden").click(function () {
$("#conditionHidden").val("true");
});
$(".descriptionForHidden").change(function () {
$("#descriptionHidden").val("true");
});
$(".overallConditionForHidden").change(function () {
$("#overallConditionHidden").val("true");
});
// Small portion of the controller
[HttpPost]
[ValidateAntiForgeryToken]
[CustomAuthorize(Module = CustomAttributes.Module.Reports, Permissions = new[] { CustomAttributes.Permission.Edit })]
public ActionResult EditReportDetail(ReportDetailModel model, string alertHidden, string noticesHidden, string statusHidden, string conditionHidden, string descriptionHidden, string overallConditionHidden)
{
CreateAndSendMessage(model, alertHidden, noticesHidden, statusHidden, conditionHidden, descriptionHidden, overallConditionHidden);
}
public bool CreateAndSendMessage(ReportDetailModel model, string alertHidden, string noticesHidden, string statusHidden, string conditionHidden, string descriptionHidden, string overallConditionHidden)
{
var locationState = "";
switch (model.LocationState)
{
case 1:
locationState = "Closed";
break;
case 2:
locationState = "Partially Open";
break;
case 3:
locationState = "Open";
break;
}
var reportName = "<p><b>Report Name:</b> " + model.ReportName + "</p><br><br>";
var alert = "<p><b>Alert:</b> " + model.Alert + "<p><br><br>";
var notices = "<p><b>Alert Details:</b> " + model.Notices + "</p><br><br>";
var state = "<p><b>Status:</b> " + locationState + "</p><br><br>";
var condition = "<p><b>Condition:</b> " + model.Condition + "</p><br><br>";
var trailCondition = "<p><b>Overall Trail Condition:</b> " + model.TrailCondition + "</p><br><br>";
var description = "<p><b>Description:</b> " + model.Description + "</p><br><br>";
var _bodyBuilder = new StringBuilder();
//_bodyBuilder.AppendFormat("<p><b>Report Name:</b> " + model.ReportName + "</p><br><br>");
_bodyBuilder.AppendFormat(reportName);
if (alertHidden == "true")
{
//_bodyBuilder.AppendFormat("<p><b>Alert:</b> " + model.Alert + "<p><br><br>");
_bodyBuilder.AppendFormat(alert);
}
if (noticesHidden == "true")
{
//_bodyBuilder.AppendFormat("<p><b>Alert Details:</b> " + model.Notices + "</p><br><br>");
_bodyBuilder.AppendFormat(notices);
}
if (statusHidden == "true")
{
//_bodyBuilder.AppendFormat("<p><b>Status:</b> " + state + "</p><br><br>");
_bodyBuilder.AppendFormat(state);
}
if (conditionHidden == "true")
{
//_bodyBuilder.AppendFormat("<p><b>Condition:</b> " + model.Condition + "</p><br><br>");
_bodyBuilder.AppendFormat(condition);
}
if (overallConditionHidden == "true")
{
//_bodyBuilder.AppendFormat("<p><b>Overall Trail Condition:</b> " + model.TrailCondition + "</p><br><br>");
_bodyBuilder.AppendFormat(trailCondition);
}
if (descriptionHidden == "true")
{
//_bodyBuilder.AppendFormat("<p><b>Description:</b> " + model.Description + "</p><br><br>");
_bodyBuilder.AppendFormat(description);
}
var fromName = ConfigurationManager.AppSettings["CMSFromName"];
var fromEmail = ConfigurationManager.AppSettings["CMSFromEmail"];
try
{
var message = new MailMessage();
message.To.Add(new MailAddress("[email protected]", "admin"));
message.From = new MailAddress(fromEmail, fromName);
message.Subject = "Report Update - " + model.ReportName;
message.Body = _bodyBuilder.ToString();
message.IsBodyHtml = true;
EmailService smtp = new EmailService();
smtp.SendMessage(message);
}
catch (Exception)
{
ModelState.AddModelError(String.Empty, "Error sending email.");
}
return true;
}