邮件发送验证码

时间:2022-06-01 18:59:35
 if (!string.IsNullOrEmpty(this.txtMail.Text))
{
try
{
//声明一个Mail对象
MailMessage mymail = new MailMessage();
//发件人地址
//如是自己,在此输入自己的邮箱
mymail.From = new MailAddress(ConfigurationManager.AppSettings["MEAILADDRESS"].ToString());
//收件人地址
mymail.To.Add(new MailAddress(this.txtMail.Text));
//邮件主题
mymail.Subject = "用户绑定验证码(请勿回复此邮件)";
//邮件标题编码
mymail.SubjectEncoding = System.Text.Encoding.UTF8;
//发送邮件的内容
string checkCode = new Random().Next(100000, 1000000).ToString();
if (Session["CheckCode"] == null)
{
Session["CheckCode"] = checkCode;
}
mymail.Body = "亲爱的用户<br/><br/>您好,您本次验证码为:" + checkCode + ",该验证码有效期为" + ConfigurationManager.AppSettings["TIMEOUT"].ToString() + "分钟,切勿告诉他人。";
//邮件内容编码
mymail.BodyEncoding = System.Text.Encoding.UTF8;
//是否是HTML邮件
mymail.IsBodyHtml = true;
//邮件优先级
mymail.Priority = MailPriority.High;
//创建一个邮件服务器类
SmtpClient myclient = new SmtpClient();
myclient.Host = "smtp.163.com";
//SMTP服务端口
myclient.Port = 25;
//验证登录
myclient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["MEAILADDRESS"].ToString(), ConfigurationManager.AppSettings["MEAILPASSWORD"].ToString());//"@"输入有效的邮件名, "*"输入有效的密码
myclient.Send(mymail);
this.lblSend.Text = "验证码已发送到您的邮箱,请登录邮箱查看";
}
catch (Exception)
{
this.lblSend.Text = "验证码获取失败,请重新获取";
}

ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>myfun();</script>");
}
else
{
this.lblMsg.Text = "请输入邮箱!";
}