Bellow is the code for a button where i am trying to execute an email valiadator before and email is sent. The if statement works great until i enter the msgmail code in it. then i get an invalid expression error.

Can someone please help me with this??

thanks

protected void Button1_Click(object sender, EventArgs e)
    {

        string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
        Regex reLenient = new Regex(patternLenient);

        bool isLenientMatch = reLenient.IsMatch(txtEmail.Text);

         MailMessage msgMail = new MailMessage();


         if (isLenientMatch)

                 msgMail.To = "aaaaa@gmail.com";
            msgMail.From = txtEmail.Text;
            msgMail.Subject = txtSubject.Text;

            msgMail.BodyFormat = MailFormat.Html;
            msgMail.Body = txtBody.Text + " <html><body><br /><br /></body></html>" + txtName.Text;



            SmtpMail.SmtpServer = "smtp1r.cp.blacknight.com";
            SmtpMail.Send(msgMail);
        


             this.ClientScript.RegisterStartupScript(this.GetType(), "Thank You", "<script language=\"javaScript\">" + "alert('Thank for your query, we will contact you within the next 24hours.');" + "window.location.href='Contact.aspx';" + "<" + "/script>");

         else


             this.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script language=\"javaScript\">" + "alert('Please enter a valid email address to proceed.');" + "<" + "/script>");

         txtEmail.Focus();

Recommended Answers

All 2 Replies

Why go through the trouble of using Regex? Let the CLR handle validation for you which will solve your invalid pattern error as well:

public static bool IsValidEmailAddr(string email)
    {
      try
      {
        System.Net.Mail.MailAddress addr = new System.Net.Mail.MailAddress(email);
      }
      catch
      {
        return false;
      }
      return true;
    }

If this is not the problem you were referring to then please use CODE TAGS and mark the line of code that is causing the problem.

I think I misread your first post. Use {} to tell the compiler it is a code block and not a single statement.

if (condition)
{
  //do stuff
}
else
{
 //do different stuff
}

Your code:

protected void Button1_Click(object sender, EventArgs e)
{
  string patternLenient = @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
  Regex reLenient = new Regex(patternLenient);
  bool isLenientMatch = reLenient.IsMatch(txtEmail.Text);
  MailMessage msgMail = new MailMessage();
  if (isLenientMatch)
  {
    msgMail.To = "aaaaa@gmail.com";
    msgMail.From = txtEmail.Text;
    msgMail.Subject = txtSubject.Text;
    msgMail.BodyFormat = MailFormat.Html;
    msgMail.Body = txtBody.Text + " <html><body><br /><br /></body></html>" + txtName.Text;
    SmtpMail.SmtpServer = "smtp1r.cp.blacknight.com";
    SmtpMail.Send(msgMail);
    this.ClientScript.RegisterStartupScript(this.GetType(), "Thank You", "<script language=\"javaScript\">" + "alert('Thank for your query, we will contact you within the next 24hours.');" + "window.location.href='Contact.aspx';" + "<" + "/script>");
  }
  else
  {
    this.ClientScript.RegisterStartupScript(this.GetType(), "Error", "<script language=\"javaScript\">" + "alert('Please enter a valid email address to proceed.');" + "<" + "/script>");
    txtEmail.Focus(); 
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.