获取验证码效果和后台代码,js+html+cs

客户端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 />

  

服务端代码:

/// <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";//异常
            }
        }