客戶端js+html代碼
<script type="text/javascript"> var tcode = 0;//定時器返回代碼 //獲得驗證碼 function GetVerifyCodeAction() { var email = $("#email").val(); if (!checkEmail(email)) { $("#area_error").addClass("log-tips").show().text(EmailFormatIsFault);//郵箱格式錯誤 } else { $.ajax({ type: "post", async: false, url: "/Handler/SendMsgToMail.ashx", data: { op: 12, email: email }, success: function (result) { var data = parseInt(result); switch (data) { case 1: $("#area_error").addClass("log-tips").show().text(CheckVerifyCode);//驗證碼已發送,請注意查收 $("#btnSendCode").removeAttr("onclick");//移除發送驗證碼的click事件 tcode = setInterval("ReSend()", 1000);//設置定時器,60秒后容許再次發送 break; case -1: $("#area_error").addClass("log-tips").show().text(FillEmail);//請輸入郵箱 break; case -2: $("#area_error").addClass("log-tips").show().text(MailNotReg);//郵箱尚未注冊 break; case -3: $("#area_error").addClass("log-tips").show().text(OperateException);//操作異常 break; case -4: $("#area_error").addClass("log-tips").show().text(OperateException);//操作異常 break; case -5: $("#area_error").addClass("log-tips").show().text(OnceMinute);//每分鍾只能發送一次 break; default: $("#area_error").addClass("log-tips").show().text(OperateException);//操作異常 break; } } }); } return false; } //點擊進入下一步 function GoNext() { $("#area_error").removeClass("log-tips").text("").hide(); var email = $("#email").val(); if (!checkEmail(email)) { $("#area_error").addClass("log-tips").show().text(EmailFormatIsFault);//郵箱格式不正確 return false; } var vcode = $("#verify").val(); if (vcode == "") { $("#area_error").addClass("log-tips").show().text(FillVerifyCode);//請輸入驗證碼 return false; } //判斷驗證碼是否正確 $.ajax({ type: "post", url: "/Handler/Members.ashx", data: { op: 14, email: email, vcode: vcode }, success: function (result) { if (result == "-1") { $("#area_error").addClass("log-tips").show().text(VCodeIsNotAvailable);//驗證碼已失效 } if (result == "1") { window.location = "forgot_password.aspx?email=" + email + "&vcode=" + vcode; } } }); } //定時器 function ReSend() { var Wait60Second="60秒后重發"; var TotalCount = $("#hf_timecount").val(); TotalCount = TotalCount - 1; $("#hf_timecount").val(TotalCount); if (TotalCount == 0) { ReSetSendMail(); } else { $("#btnSendCode").text(Wait60Second.replace("60", TotalCount)); } }
//重新附加發送郵箱事件 function ReSetSendMail() { clearInterval(tcode); $("#hf_timecount").val("60"); $("#btnSendCode").text("獲取驗證碼"); $("#btnSendCode").attr("onclick", "GetVerifyCodeAction()"); } </script>
<input id="hf_timecount" value="60" type="hidden" />
<input type="text" name="email" id="email" />
<button type="button" id="btnSendCode" onclick="GetVerifyCodeAction()">獲取驗證碼</button>
<input type="text" name="verify" id="verify" />
<input type="button" id="btn_next" value="下一步" onclick="GoNext()"/>
服務端代碼:
/// <summary> /// 發送郵件 /// </summary> /// <param name="context"></param> /// <returns></returns> public string SendMail(HttpContext context) { try { if (!string.IsNullOrEmpty(CookiesHelper.getCookie("send_mail_limit"))) { return "-5";//每分鍾只能發送一次 } string email = context.Request["email"]; if (string.IsNullOrEmpty(email) || !CommonHelper.IsValidEmail(email)) { return "-1";//傳值為空 } //判斷郵件是否存在 BLL.Web.Member bllMember = new BLL.Web.Member(); int mailCount = bllMember.GetCountByEmail(email); Models.Web.Member member = bllMember.GetModelByEmail(email); if (mailCount == 0 || member == null) { return "-2";//不存在 } string vcode = CommonHelper.RandCode(8); Models.Web.VerifyCode model = new Models.Web.VerifyCode(); model.v_code = vcode; model.v_createdate = DateTime.Now; model.v_enddate = DateTime.Now.AddHours(2); model.v_status = 0; model.v_email = email; BLL.Web.VerifyCode bllVC = new BLL.Web.VerifyCode(); int no = bllVC.Append(model); if (no > 0) { string sendText = ""; string tempPath = context.Server.MapPath("~/EmailTemp/ModifyPwd.txt"); using (StreamReader sr = new StreamReader(tempPath)) { sendText = sr.ReadToEnd(); } sendText = sendText.Replace("{UserName_CH}", member.PersnalName); sendText = sendText.Replace("{UserName_EN}", member.PersnalName); sendText = sendText.Replace("{VCode}", vcode); CommonHelper.SendEmail(email, sendText, Resource.Lang.RetrievePassword); CookiesHelper.setCookie("send_mail_limit", "SendMail", 1.00); } else { return "-3";//驗證碼生成異常,請重試! } return "1";//成功 } catch (Exception) { return "-4";//異常 } }