1、App_Data目录新建模板文档(目录随意选)
1 <!DOCTYPE html> 2 <html> 3 <body> 4 <p>亲爱的{{Name}},您好:</p> 5 <p> 6 欢迎您注册成为本站用户,为了完成用户注册程序,请点击以下链接用以确认您的Email地址是有效的:<br /> 7 <a href="{{AUTH_URL}}" target="_blank">{{AUTH_URL}}</a> 8 </p> 9 <p>这是一封自动产生的email,请勿回复。</p> 10 </body> 11 </html>
2、替换文档字符,生成并发送邮件
1 public class Email 2 { 3 public void SendAuthenticateEmail(string email,string salt) 4 { 5 string mailBody = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Data/RegisterEmail.htm")); 6 mailBody = mailBody.Replace("{{Name}}", email); 7 mailBody = mailBody.Replace("{{AUTH_URL}}", "url?validate=" + salt); 8 SmtpClient smtpServer = new SmtpClient("smtp.163.com"); 9 smtpServer.Port = 587; 10 //smtpServer.ClientCertificates = new System.Net.NetworkCredential("email", "password"); 11 smtpServer.EnableSsl = true; 12 13 MailMessage mail = new MailMessage(); 14 mail.From = new MailAddress("email"); 15 mail.To.Add(email); 16 mail.Subject = "网站名称+会员注册确认信"; 17 mail.Body = mailBody; 18 mail.IsBodyHtml = true; 19 smtpServer.Send(mail); 20 } 21 }
3、点击通过验证
1 public ActionResult AuthenticateEmail(string validate,string email) 2 { 3 Guid id=db.Users.FirstOrDefault(u=>u.Email==email).UserId; 4 string salt=db.Profiles.SingleOrDefault(p=>p.ProfileId==id).Salt; 5 if (validate == salt) 6 { 7 db.Users.Find(id).IsActive = true; 8 db.SaveChanges(); 9 } 10 else 11 { 12 TempData["Error"] = "无效的验证码或您已经通过验证。"; 13 } 14 return RedirectToAction("Index", "Profiles"); 15 }
时间: 2024-12-28 01:44:27