using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Text; using System.Net.Mail; using System.Net; namespace Web.Common { ///<summary> /// 找回密码类 ///</summary> public class MyEmail { #region 找回密码 ///<summary> /// 功能:用户找回密码 ///</summary> ///<param name="ToEmail">目的地地址</param> ///<param name="memberid">会员ID</param> ///<returns></returns> public static bool SendMail(string ToEmail,string memberid) { string Email = "m.sh.lin0328@163.com"; string password = "abcd"; Encoding EnCode = Encoding.UTF8; System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage(); Message.From = new MailAddress(Email, "新郑网购会员管理中心", EnCode); Message.To.Add(new MailAddress(ToEmail, "尊敬的用户", EnCode)); Message.Subject = "新郑网购网找回密码"; Message.SubjectEncoding = EnCode; //CC为抄送,即发给用户的同时,再发送给Email //Message.CC.Add(new MailAddress(Email,"房产巴巴网")); StringBuilder MailContent = new StringBuilder(); MailContent.Append("尊敬的用户:<br/>"); MailContent.Append(" 您好!你于"); MailContent.Append(DateTime.Now.ToLongTimeString()); MailContent.Append("通过<a href='#'>新郑网购</a>管理中心审请找回密码。"); MailContent.Append("<br/> 为了安全起见,请用户点击以下链接重设个人密码:"); string url = "http://www.hnjcg.com/getPass.aspx?memberid=" + memberid + "&email=" + ToEmail; MailContent.Append("<br/> <a href='" + url + "'>" + url + "</a>"); Message.Body = MailContent.ToString(); Message.BodyEncoding = EnCode; Message.IsBodyHtml = true; try { SmtpClient smtp = new SmtpClient("smtp.163.com", 25); smtp.Credentials = new NetworkCredential(Email, password); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Send(Message); } catch (Exception) { return false; } finally { Message.Dispose(); } return true; } #endregion } } 接收邮箱返回数据并重置密码: string memeberid = "";//用来接收找回密码时返回的email地址,与会员编号 protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["email"] != null) { email = Request.QueryString["email"].ToString(); } if (Request.QueryString["memberid"] != null) { memeberid = Request.QueryString["memberid"].ToString(); modelMember = bllMember.GetModel(long.Parse(memeberid)); modelMember.PassWord = TianDao.Common.DEncrypt.DESEncrypt.Encrypt("123456"); if (modelMember.Email == email)//验证邮箱地址是否为用户注册时候的邮箱地址 { bllMember.Update(modelMember); Response.Write("<script>alert('您的密码已重置,默认密码为123456,请尽快修改密码!');window.location.href='../login.aspx';</script>"); } } if (!IsPostBack) { } }
/// <summary> /// 隐藏部分邮箱名 /// </summary> /// <param name="source"></param> /// <returns></returns> public static string HideEmail(string email) { string[] em = email.Split(new char[] { '@' }, StringSplitOptions.RemoveEmptyEntries); string em2 = em[0].ToString(); string em3 = ""; string em4 = ""; if (em2.Length > 3) { for (int i = 0; i < em2.Length - 3; i++) { em4 += "*"; } em3 = em2.Substring(0, 3) + em4 + "@" + em[1].ToString(); } else { em3 = "***" + "@" + em[1].ToString(); } return em3; } /// <summary> /// 验证邮箱是否合法 /// </summary> /// <param name="source"></param> /// <returns></returns> public static bool IsEmail(string source) { return Regex.IsMatch(source, @"^[A-Za-z0-9](([_.-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([.-]?[a-zA-Z0-9]+)*).([A-Za-z]{2,})$", RegexOptions.IgnoreCase); }
使用正则表达式类(Regex Class) 如果你没有使用服务器端的控件(意味着你不能使用验证控件),或者你需要其它的输入字段源而非表单字段(比如查询字串参数和cookies),那么你可以使用正则表达式类(Regex class). 使用正则表达式类 加入使用using前缀的语句导入System.Text.RegularExpressions命名空间. 确认正则表达式包含"^"和"$"(字串开始处,字串结束处). 调用Regex类的IsMatch方法,下面是代码示例. // Instance method: Regex reg = new Regex(@"^[a-zA-Z'.\s]{1,40}$"); Response.Write(reg.IsMatch(txtName.Text)); // Static method: if (!Regex.IsMatch(txtName.Text,@"^[a-zA-Z'.\s]{1,40}$")) { // Name does not match expression } 如果你不能把经常使用的正则表达式缓存起来,你应该使用IsMatch静态方法来改进性能防止不必要的对象创建过程.