如果之前已经发布过,我很抱歉。我已经搜索了许多网站和表格来修复它,但我无法得到它。我有一个简单的联系表格,允许潜在客户填写他们的信息点击提交,然后通过电子邮件将他们输入的内容的副本发送给我们。我的电子邮件部分工作正常。但是,不工作的部分是表单提交后的消息。我尝试使用 try 和 catch 在他们提交时显示一条消息,或者在它不起作用时显示一条错误消息。不知道为什么它不工作。感谢您的帮助。我的控制器代码如下。
public ActionResult ContactForm()
{
return View();
}
public ActionResult Message()
{
return View();
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult ContactForm(ContactModel emailModel)
{
if (ModelState.IsValid)
{
bool isOk = false;
try
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("no-reply@bobcravens.com", "Website Contact Form");
msg.To.Add("thovden@hovdenoil.com");
msg.Subject = emailModel.Subject;
string body = "Name: " + emailModel.Name + "\n"
+ "Email: " + emailModel.Email + "\n"
+ "Website: " + emailModel.Website + "\n"
+ "Phone: " + emailModel.Phone + "\n\n"
+ emailModel.Message;
msg.Body = body;
msg.IsBodyHtml = false;
SmtpClient smtp = new SmtpClient("smtpout.server.net", 25);
NetworkCredential Credentials = new NetworkCredential("thovden@hovdenoil.com", "****");
smtp.Credentials = Credentials;
smtp.Send(msg);
msg.Dispose();
isOk = true
ContactModel rcpt = new ContactModel();
rcpt.Title = "Thank You";
rcpt.Content = "Your email has been sent.";
return View("Message", rcpt);
}
catch (Exception ex)
{
}
// If we are here...something kicked us into the exception.
//
ContactModel err = new ContactModel();
err.Title = "Email Error";
err.Content = "The website is having an issue with sending email at this time. Sorry for the inconvenience. My email address is provided on the about page.";
return View("Message", err);
}
else
{
return View();
}
}
}