Sending email in ASP.NET

How to write a "Contact us" form in ASP.NET

private void SendButton_Click(object sender, EventArgs e)
{
  if (NameTextBox.Text.Length == 0)
    AddError("You must enter your name");
  if (EmailAddressTextBox.Text.Length == 0)
    AddError("You must enter your email address");
  if (CommentsTextBox.Text.Length == 0)
    AddError("You must enter some comments");

  System.Text.RegularExpressions.Regex checkEmail = new System.Text.RegularExpressions.Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
  if (!checkEmail.IsMatch(EmailAddressTextBox.Text))
    AddError("You must enter a valid email address");

  if (!HasErrors)
  {
    System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
    mail.From = NameTextBox.Text + " <" + EmailAddressTextBox.Text + ">";
    mail.To = System.Configuration.ConfigurationSettings.AppSettings["EmailAddress"];


    mail.Subject = "Site feedback";
    mail.BodyFormat = System.Web.Mail.MailFormat.Text;
    mail.Body = CommentsTextBox.Text;
    System.Web.Mail.SmtpMail.Send(mail);
    AddMessage("Your feedback has been sent, thank you.");
    Response.Redirect("/", false);
  }
}

 

Share this article!

Follow us!

Find more helpful articles: