1
我有我的mvc網站反饋表格,我發送此表格到電子郵件。
我希望在電子郵件發送失敗時顯示錯誤消息,並在電子郵件發送成功時顯示成功消息。我試圖通過ViewBag做到這一點。
我在我的控制器顯示消息「電子郵件發送失敗/成功」asp.net mvc 4通過ViewBag
[HttpGet]
public ActionResult Feedback(string Message)
{
if (Message != null)
{
if (Message == "No")
{
ViewBag.Message = "Error";
}
if (Message == "Yes")
{
ViewBag.Message = "Success";
}
}
else
{
ViewBag.Message = null;
}
return View();
}
[HttpPost]
public ActionResult Feedback(FeedbackForm Model)
{
string Message;
//email
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.BodyEncoding = Encoding.UTF8;
msg.Priority = MailPriority.High;
msg.From = new MailAddress(Model.Email, Model.Name);
msg.To.Add(/*"[email protected]"*/"[email protected]");
msg.Subject = @Resources.Global.Feedback_Email_Title + " " + Model.Company;
string message = @Resources.Global.Feedback_Email_From + ": " + Model.Name + "\n"
+ @Resources.Global.Feedback_Email + ": " + Model.Email + "\n"
+ @Resources.Global.Feedback_Phone + ": " + Model.Phone + "\n"
+ @Resources.Global.Feedback_Company + ": " + Model.Company + "\n\n"
+ Model.AdditionalInformation;
msg.Body = message;
msg.IsBodyHtml = false;
//Attachment
if (Model.ProjectInformation != null && !(String.IsNullOrEmpty(Model.ProjectInformation.FileName)))
{
HttpPostedFileBase attFile = Model.ProjectInformation;
if (attFile.ContentLength > 0)
{
var attach = new Attachment(attFile.InputStream, attFile.FileName);
msg.Attachments.Add(attach);
}
}
SmtpClient client = new SmtpClient("denver.corepartners.local", 55);
client.UseDefaultCredentials = false;
client.EnableSsl = false;
try
{
client.Send(msg);
return RedirectToAction("Feedback", "Home", Message = "Yes");
}
catch (Exception ex)
{
return RedirectToAction("Feedback", "Home", Message = "No");
}
}
添加和我在我看來
@if (ViewBag.Message != null)
{
<p style="color: red;">@ViewBag.Message</p>
}
添加,但我沒有在任何情況下得到任何消息。
有什麼問題?
感謝的很多,它的作品! – Heidel