0

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;
}
}

1 Answer 1

1

You forgot to check Page.IsValid at the start of the Button1_Click.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.