似乎当我在联系页面后面的代码中配置我的电子邮件设置时,验证控件不再起作用。我需要字段验证和正则表达式验证来验证电子邮件地址。在我插入要求我使用 System.Net.Mail 的电子邮件配置之前,验证适用于点击事件。这是后面的代码:
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@gmail.com");
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@gmail.com", "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;
}
}