It seems that when I configure my email settings in the code behind for a contact page the validation controls no longer work. I have required field validation and regular expression validation to validate email addresses. Validation works on click event before I insert email configurations which requires me to use System.Net.Mail. Here is the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To.Add("[email protected]");
mail.From = new MailAddress(EmailAddressTextBox.Text);
mail.Subject = SubjectTextBox.Text;
mail.Body = "email address: " + EmailAddressTextBox.Text + "<br />" + MessageTextBox.Text;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
smtp.Credentials = new System.Net.NetworkCredential
("[email protected]", "password");
//Or your Smtp Email ID and Password
smtp.EnableSsl = true;
smtp.Port = 587;
smtp.Send(mail);
EmailAddressTextBox.Text = String.Empty;
SubjectTextBox.Text = String.Empty;
MessageTextBox.Text = String.Empty;
}
}