欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

.net 邮箱验证码 邮箱验证码实现用户注册

程序员文章站 2022-05-13 09:50:40
...

.net邮箱注册
前端时间做项目,手机短信验证需要购买服务,而且容易被工具.
纠结之后,用了邮箱验证.
直接上代码

/// <summary>
        /// 同步发送邮件
        /// </summary>
        /// <param name="to">收件人邮箱地址</param>
        /// <param name="subject">主题</param>
        /// <param name="body">内容</param>
        /// <param name="encoding">编码</param>
        /// <param name="isBodyHtml">是否Html</param>
        /// <param name="enableSsl">是否SSL加密连接</param>
        /// <returns>是否成功</returns>
        public static bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false)
        {
             
            //try
            //{
                MailMessage message = new MailMessage();
                // 接收人邮箱地址
                message.To.Add(new MailAddress(to));
                message.From = new MailAddress(MailUserName, MailName);
                message.BodyEncoding = Encoding.GetEncoding(encoding);
                message.Body = body;
                //GB2312
                message.SubjectEncoding = Encoding.GetEncoding(encoding);
                message.Subject = subject;
                message.IsBodyHtml = isBodyHtml;

                SmtpClient smtpclient = new SmtpClient(MailServer, 587);
                smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword);
                //SSL连接
                smtpclient.EnableSsl = enableSsl;
                smtpclient.Send(message);
                return true;
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(ex.ToString());
            //    return false;
            //}
        }
[HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SendEmail(VerificationEntity entity)
        {
            VerificationEntity model = new VerificationEntity();
            if (entity.Telphone.IsEmpty())
                return Info("请输入正确邮箱地址");
            //if (entity.CodeType == 3)
            //{
            //   var  Cmodel = cuserBll.CheckCUser(entity.Telphone);
            //    if(Cmodel==null)
            //        return Info("账号不存在,输入正确的账号!");
            //}
            model.Telphone = entity.Telphone;
            model.CodeType = entity.CodeType;
            model.Type = 1;
            string ret = verificationBLL.AddCode(model);
            if (ret == "")
            {
                try
                {
                    bool flag = false;
                    if (model.CodeType == 1)
                          flag = MailHelper.Send(entity.Telphone, "28找群注册验证码通知", "注册验证码:" + model.Content + " 30分钟内有效,请输入验证码,完成注册<br>www.28zhaoqun.com 资源不断,人脉无线", "UTF-8", true, true);
                    else if (model.CodeType == 3)
                        flag = MailHelper.Send(entity.Telphone, "28找群密码找回验证码通知", "密码找回验证码:" + model.Content + " 30分钟内有效,请输入验证码,完成操作<br>www.28zhaoqun.com 资源不断,人脉无线", "UTF-8", true, true);
                    if (!flag)
                        return Error("邮件发送失败,请检查邮箱地址是否正确");
                }
                catch (Exception ex)
                {
                    //return Error(ex.Message);
                    throw;
                }
                return Success("验证码已经发送,请进入邮箱查看验证码。");
            }
            else
                return Error(ret);
        }