AI智能
改变未来

ASP.net应用QQ邮箱发送验证码

using 引用

using System;using System.Collections.Generic;using System.Linq;using System.Net.Mail;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Text.RegularExpressions;//验证邮箱的正则表达using BLL;using Model;

封装邮箱验证码方法

//封装邮箱验证码方法public bool FsEmail(string biaotou, string neirong, string Emailta){string yanzm = neirong.ToString();string emailwod = \"[email protected]\";//设置要发送邮件的email账号,(自己的邮箱号)string password = \"klfpintmpuihbccd\";//邮箱POP3/SMTP服务授权码string subject = \"smtp.qq.com\";     //QQ邮箱特有的 //邮箱特有的编码SmtpClient client = new SmtpClient(subject); //应该是实例化这个编码client.Credentials = new System.Net.NetworkCredential(emailwod, password);//创建凭据包含用户名和密码client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; //指定发送电子邮件的途径,通过网络发送到SMTP服务器client.EnableSsl = true; //指定用户使用ssl安全链接(对于smtp设置为安全链接的必须设置为true)MailMessage mail = new MailMessage(emailwod, Emailta, biaotou, yanzm);//建立邮件对象:发送邮件账号,邮件发送地址,邮件主题,邮件正文mail.BodyEncoding = System.Text.Encoding.UTF8;//定义邮件正文的编码方式mail.IsBodyHtml = true;//指定正文是否为html元素mail.Priority = System.Net.Mail.MailPriority.High;//指定邮件优先级try{client.SendAsync(mail, \"\");//使用异步发送方法return true;//发送成功返回true}catch (Exception yc){throw yc; //应该是抛出异常}}

获取验证码点击事件

//获取验证码点击事件protected void But_hqyanzhengma_Click(object sender, EventArgs e){//获取邮箱验证string email = TextB_emailb.Text;if (email != \"\"){//实例化一个Regex对象Regex re = new Regex(@\"[\\w!#$%&\'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&\'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?\");if (re.IsMatch(email) == true){//Response.Write(\"<script>alert(\'邮箱正确\')</script>\");//进行判断确保是qq邮箱string newemail = email.Substring(email.LastIndexOf(\"@\")).ToLower();if (newemail == \"@qq.com\"){//输入的是qq邮箱  掉方法 发送验证码//查询数据库看账号是否存在useraBLL useraBLL = new useraBLL();if (useraBLL.selectisNull(email)){string biaotou = \"九离欢迎您\";Random random = new Random();string yanzhegma = random.Next(1111, 9999).ToString();//验证码保存到文本框TextB_hide.Text = yanzhegma;//邮箱保存到文本框TextB_hideemail.Text = email;//定义内容string neirong = \"您的验证码为:\" + yanzhegma;//调用方法发送邮箱if (FsEmail(biaotou, neirong, email)){Response.Write(\"<script>alert(\'注意查收验证码\')</script>\");}else{Response.Write(\"<script>alert(\'未知错误\')</script>\");}}else{Response.Write(\"<script>alert(\'邮箱已经存在\')</script>\");}}else{Response.Write(\"<script>alert(\'请用qq邮箱登录\')</script>\");}}else{Response.Write(\"<script>alert(\'邮箱错误\')</script>\");}}else{Response.Write(\"<script>alert(\'请输入qq邮箱\')</script>\");}}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » ASP.net应用QQ邮箱发送验证码