天使将会展现网站编程最真实的一面,包括编程中遇到的一些错误指令的处理,网站调试过程,排查代码错误等,通过本系列经验,相信你在.net网站开发方面会有一个很大的提升。同时也希望小伙伴们喜欢天使的经验,把我的经验分享给更多需要帮助的人。后面功能模块比较深入,大家不懂的可以先看看前面的经验。
工具/原料
为了避免用户重复劳作,使我们发一些错误邮件,我们首先验证用户名是否存在,输完用户名之后,我们调用ontextchanged事件
string sqlcon = "select * from [user] where user_name='" + txtName.Text + "' ";
if (myclass.IsSameRecord(sqlcon))
{
lblMsg.Text = "";
}
else
lblMsg.Text = "用户名不存在";
在【User_ForgetPassword.aspx】页面上添加引用
using System.Web.Mail;
添加一个发送电子邮件的邮箱,在代码里面填好用户名和密码
#region 发邮件TextBox9 private bool SendMail(string fromMail, string toMail, string ccMail, string bccMail, string subject, string body) { try { MailMessage myMail = new MailMessage();
myMail.From = fromMail; myMail.To = toMail; myMail.Subject = subject; myMail.Body = body; string from = ""; string password = ""; myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1); myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", from); //发送方邮件帐户 myMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); //发送方邮件密码
SmtpMail.SmtpServer = "smtp." + fromMail.Substring(fromMail.IndexOf("@") + 1); SmtpMail.Send(myMail);
return true; } catch { return false; }
}
#endregion
在按钮的Click事件中,添加发送邮件代码
try { bool flag = SendMail("邮箱地址", txtEmail.Text, "", "", "密码找回", "恭喜您成功注册新闻发布系统");
if (flag == true) { Response.Write("<script>alert('发送邮件成功!');</script>"); } else { Response.Write("<script>alert('发送失败!');</script>"); } string mes = "已经将用户密码发至您的邮箱,请注意查收!"; Response.Write("<script>alert('" + mes + "');location='login.aspx'</script>");
} catch { } finally { }