SMTP 服务器要求安全连接或客户端未通过身份验证。
protected void Cmd_Reg_Click(object sender, EventArgs e)
{
Random random = new Random();
string checkCode = "";
for (int i = 0; i <= 4; i++)
{
int num = random.Next(1, 10);
checkCode += num.ToString();
}
string validataCode = FormsAuthentication.HashPasswordForStoringInConfigFile(checkCode, "md5");
SqlConnection conn = new SqlConnection(str);
conn.Open();
string sql = "insert into Email(UserId,UserPas,mail,AddDate,ValidataCode,UserState) values(@UserId,@UserPas,@mail,@AddDate,@ValidataCode,@UserState)";
SqlCommand cmd = new SqlCommand(sql,conn);
SqlParameter[] param = {
new SqlParameter("@UserId",Txt_Name.Text),
new SqlParameter("@UserPas",Txt_Pwd.Text),
new SqlParameter("@mail",Txt_Email.Text),
new SqlParameter("@AddDate",DateTime.Now),
new SqlParameter("@ValidataCode", validataCode),
new SqlParameter("@UserState",false),
};
cmd.Parameters.AddRange(param);
int result = cmd.ExecuteNonQuery();
conn.Close();
if (result > 0)
{
ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('ok')</script>");
string strSmtpServer = "smtp.gmail.com";
string strFrom ="358730096@qq.com";
string strFromPass = "123456";
string strto = Txt_Email.Text;
string strSubject = "账号激活";
StringBuilder strBody = new StringBuilder();
strBody.Append("点击下面链接激活账号,48小时生效,否则就要重新注册账号,链接只能使用一次,请尽快激活!</br>");
strBody.Append("<a href='http://localhost:2493/web/Operate.aspx?userName=" + Txt_Name.Text + "&validateCode=" + validataCode + "'>点击这里</a></br>");
strBody.Append("如未能激活请点击下面链接:<a href='http://localhost:2493/web/Operate.aspx?userName=" + Txt_Name.Text + "&validateCode=" + validataCode + "'>http://localhost:2493/web/Operate.aspx?userName=" + Txt_Name.Text + "&validateCode=" + validataCode + "</a></br>");
SendSMTPEMail(strSmtpServer, strFrom, strFromPass, strto, strSubject, strBody.ToString());
}
else
{
ClientScript.RegisterStartupScript(this.Page.GetType(), "alert", "<script>alert('Nok')</script>");
}
}
//发送邮件
public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
{
MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody);
message.BodyEncoding = System.Text.Encoding.UTF8;
message.IsBodyHt