AI智能
改变未来

C#实现QQ邮箱发送验证码 Api+Mvc

Dal层sql语句

public string QQEmailCode(string email)//设定收件人邮箱地址参数{int randNum;char code;string randomcode = string.Empty;//随机验证码//生成6位数验证码for (int i = 0; i < 6; i++){byte[] buffer = Guid.NewGuid().ToByteArray();//生成字节数组int seed = BitConverter.ToInt32(buffer,0);//利用BitConvert方法把字节数组转换为整数Random random = new Random(seed);randNum = random.Next();code = (char)(\'0\' + (char)(randNum % 10));//随机数字randomcode += code.ToString();}MailMessage mail = new MailMessage();mail.From = new MailAddress(\"3331583739@qq.com\",\"【淘宝电商平台】\");//发件人邮箱地址mail.To.Add(new MailAddress(email));//收件人邮箱地址mail.Subject = \"欢迎您注册【淘宝电商平台】\";//设置邮件标题mail.SubjectEncoding = Encoding.UTF8;//设置标题编码mail.Body = \"【淘宝电商】官方发送。\\r\\n\\r\\n您的验证码为:\"+randomcode;//邮件正题mail.BodyEncoding = Encoding.UTF8;//正文编码mail.Priority = MailPriority.High;//设置最高优先级SmtpClient client = new SmtpClient();//设置QQ邮箱的传输协议client.Host = \"smtp.qq.com\";//QQ邮箱client.Port = 587;//设置QQ邮箱端口client.EnableSsl = true;//使用安全加密ssl连接client.DeliveryMethod = SmtpDeliveryMethod.Network;//通过网络发送电子邮件到SMTP服务器client.Credentials = new NetworkCredential(\"3331583739@qq.com\",\"mqsxabxmintrchbg\");//发件人邮箱和授权码//发送邮件try{client.Send(mail);}catch (Exception ex){Console.WriteLine(ex.Message); //异常处理机制 显示报错信息}return randomcode;}

Api控制器调用Dal层方法

/// <summary>/// 通过邮箱获取验证码/// </summary>/// <param name=\"email\">收寄件人邮箱地址</param>/// <returns></returns>[HttpGet][Route(\"api/QQEmail\")]public string QQEmail(string email){return dal.QQEmailCode(email);}

Mvc对邮箱验证码匹配

//给当前页面设置Hidden隐藏 并给Id值 将发送的验证码结果存到当前页面$.ajax({url: \"http://localhost:52096/api/QQEmail?email=\" + Email, //调用Api 给邮箱发送验证码方法 并传入收件人邮箱号type: \"get\",success: function (result) {alert(\"已经发送\");$(\"#Hidden1\").val(result);}});
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C#实现QQ邮箱发送验证码 Api+Mvc