I'm a .Net noob creating a new contact form. I updated this code from a 1.1 technique to a 2.0 technique.
My first question is when the email is sent successfully, is Response.Redirect the "correct" way to move to the thank you page? (The code used to use views, but for SEO, I was asked to drop that technique.)
My second question is related to the try/catch. If I comment out the Response.Redirect in the catch, it shows the Thank You page and I get the email. If I uncomment the Response.Redirect in the catch, it goes to the error page and I still get the email. What's the right way to do the following?
Thanks!
Code:private void processForm() { String emailTo = ConfigurationSettings.AppSettings["ContactToAddress"]; String emailFrom = ConfigurationSettings.AppSettings["ContactFromAddress"]; String emailSubject = "Contact Form Submission"; String emailBody = "First Name: " + FirstName.Text + "\n" + "Last Name: " + LastName.Text + "\n" + "Company: " + Company.Text + "\n" + "Phone: " + Phone.Text + "\n" + "Email: " + Email.Text + "\n" + "Department: " + ServiceInterest.SelectedItem.Value + "\n\n" + "Comments:\n" + Comments.Text; SmtpClient client = new SmtpClient(); MailMessage email = new MailMessage(emailFrom, emailTo, emailSubject, emailBody); email.Priority = MailPriority.High; try { client.Send(email); Response.Redirect("thank-you.aspx"); } catch (Exception exc) { //literalErrorMessage.Text = exc.ToString(); Response.Redirect("contact-error.aspx"); } }







Bookmarks