0

Sorry if this has been addressed. I just can't get it to work. I have a email contact form. I have the form working ok. It is sending an email. However, after the form is submitted I was either a message to pop up or be redirected to a different page saying that their email was sent successfully and we'll respond as we can. I've tried RedirectActionTo and some other things, but I cannot get it to work. Here is my code.

 public ActionResult ContactForm(ContactModel emailModel) { MailMessage oMail = new MailMessage(); oMail.From = new MailAddress("[email protected]", "Web Contact Form"); oMail.To.Add("[email protected]"); oMail.Subject = emailModel.Subject; string body = "Name: " + emailModel.Name + "\n" + "Email: " + emailModel.Email + "\n" + "Website: " + emailModel.Website + "\n" + "Phone: " + emailModel.Phone + "\n\n" + emailModel.Message; oMail.Body = body; if (SendMessage(oMail)) { return RedirectToAction("Message"); } else { return RedirectToAction("Error"); } return View(); } private bool SendMessage(MailMessage oMail) { SmtpClient client = new SmtpClient("relay-hosting.secureserver.net"); client.Credentials = new NetworkCredential("[email protected]", "********", "domaion.com"); try { client.Send(oMail); return true; } catch (Exception ex) { this.exception = ex; return false; } } } 

Thank for your help.

1
  • @Bill Martin. Well the good news is that return RedirectToAction worked, kind of. It redirected me to the confirmation page. However, it did it right away with out giving me a chance to enter information into the form.CommentedJan 31, 2012 at 21:20

2 Answers 2

0

The reason is because the action is completed without waiting for result.

here is simplified version of my implementation

after in your controller you can set up your response such as:

public class ContactModel { public string Name { get; set; } public string Email { get; set; } public string Website { get; set; } public string Phone { get; set; } public string Message { get; set; } public string Subject { get; set; } } public ActionResult Index() { ViewData["Message"] = "Welcome to ASP.NET MVC!"; ContactModel model = new ContactModel(); model.Email = "[email protected]"; model.Message = "message"; model.Name = "name"; model.Subject = "subject"; model.Website = "http://test.com"; ContactForm(model); return View(); } /// <summary> /// Contacts the form. /// </summary> /// <returns>Returns view to display the form and fill</returns> [AcceptVerbs(HttpVerbs.Get)] public ActionResult ContactForm() { return View(new ContactModel()); } /// <summary> /// Contacts the form. /// If model is valid we will go ahead and process emailing /// </summary> /// <param name="emailModel">The email model.</param> /// <returns></returns> [AcceptVerbs(HttpVerbs.Post)] public ActionResult ContactForm(ContactModel emailModel) { if (ModelState.IsValid) { MailMessage oMail = new MailMessage(); oMail.From = new MailAddress("[email protected]", "Web Contact Form"); oMail.To.Add("[email protected]"); oMail.Subject = emailModel.Subject; string body = "Name: " + emailModel.Name + "\n" + "Email: " + emailModel.Email + "\n" + "Website: " + emailModel.Website + "\n" + "Phone: " + emailModel.Phone + "\n\n" + emailModel.Message; oMail.Body = body; if (SendMessage(oMail)) { return RedirectToAction("Message"); } else { return RedirectToAction("Error"); } } else { return View(emailModel); } } /// <summary> /// Sends the message. /// </summary> /// <param name="oMail">The o mail.</param> /// <returns>Boolean success.</returns> private bool SendMessage(MailMessage oMail) { SmtpClient client = new SmtpClient("relay-hosting.secureserver.net"); client.Credentials = new NetworkCredential("[email protected]", "********", "domaion.com"); try { client.Send(oMail); return true; } catch (Exception ex) { this.exception = ex; return false; } } 

The view is redirecting because i guess you have not split Type of the request over the method. and therefore its going straight to redirect.

this is done by

[AcceptVerbs(Http.Get)] or [AcceptVerbs(Http.Post)]

hope this helps

To pass message:

TemData["message"] = "pass message between controllers like this" 

public AcionResult Message(string message){ //here choose ViewData["message"] = TempData["message];

return View(); }

or

 return RedirectToAction("Message", new{message = "pass message between controllers"}); `enter code here` where public AcionResult Message(string message){ //or if you are using parameter ViewData["message"] = message; return View(); } 
7
  • Sorry, I'm kind of new with form code on mvc. I understand your code. Although I have a couple of questions. After "//create client" do I need anything? And after "//set all stuff you need" that's where I put the oMail stuff, but do I need "MailMessage oMail = new System.Web.Mail.MailMessage();"CommentedFeb 3, 2012 at 15:33
  • Sorry for the late response, just got back from the weekend. Quick question. I have an error, "this.exception = ex;" exception does not contain a definition. How do I correct that?CommentedFeb 6, 2012 at 17:19
  • With the code I have up above, it is still redirecting to the message page with out letting me enter in infoCommentedFeb 6, 2012 at 19:40
  • @LarsHovden: the exception does not need to be there its more precaution
    – cpoDesign
    CommentedFeb 6, 2012 at 20:07
  • ok is there a better way that to redirect? the info you manually put in the ActionResult Index() came through, but no message. And when I took out that it contact form didn't come up. Sorry for this. It's driving me nutsCommentedFeb 6, 2012 at 21:23
0

your IIS isn't set up right, this is also obsolete, you should be using the System.Net.Mail.SmtpClient instead, see SocttGu's blog post on it

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.