1.发送邮件

 public ActionResult SendEmail()
{
var member = dbSession.MemberRepository.LoadEntities(p => p.UserName == User.Identity.Name).SingleOrDefault();
var tokenCode = Send(member.ID, member.Email, member.UserName);
member.TokenCode = System.Guid.Parse(tokenCode);
dbSession.MemberRepository.Update(member);
dbSession.SaveChanges();
return View(member);
}
private string Send(int id=,string targetEmail="****@163.com",string memberName="aspen")
{
string validataCode = System.Guid.NewGuid().ToString();
try {
System.Net.Mail.MailAddress from = new System.Net.Mail.MailAddress("****@163.com", "服务贸易网"); //填写电子邮件地址,和显示名称
System.Net.Mail.MailAddress to = new System.Net.Mail.MailAddress(targetEmail, memberName); //填写邮件的收件人地址和名称
//设置好发送地址,和接收地址,接收地址可以是多个
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.From = from;
mail.To.Add(to);
mail.Subject = "主题内容"; System.Text.StringBuilder strBody = new System.Text.StringBuilder();
strBody.Append("点击下面链接激活账号,48小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!</br>");
strBody.Append("<a href='http://localhost:59637/Account/CheckEmail?memberID=" + id + "&validataCode=" + validataCode + "'>点击这里</a></br>"); mail.Body = strBody.ToString();
mail.IsBodyHtml = true;//设置显示htmls
//设置好发送邮件服务地址
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Host = "smtp.163.com";
//填写服务器地址相关的用户名和密码信息
client.Credentials = new System.Net.NetworkCredential("****@163.com", "****");
//发送邮件
client.Send(mail);
}
catch {}
return validataCode;
}

2.开启SMTP服务

要想邮件发送成功必须开通SMTP服务,以163邮箱为例:

1.登录发件人邮箱并开启SMTP服务

asp.net mvc邮箱激活-LMLPHP

asp.net mvc邮箱激活-LMLPHP

asp.net mvc邮箱激活-LMLPHP

3.将第一步中点击激活连接中的验证码与数据库中的验证码进行比对,来判断邮箱是否验证通过

 public ActionResult CheckEmail(string validataCode,int memberID=)
{
var member = dbSession.MemberRepository.LoadEntities(p=>p.ID==memberID).SingleOrDefault();
if (member.TokenCode.ToString()==validataCode)
{
TempData["CheckResult"] = "邮箱验证成功";
member.IsPassAudit = true; }
else
{
TempData["CheckResult"] = "邮箱未通过验证";
member.IsPassAudit = false;
}
dbSession.MemberRepository.Update(member);
dbSession.SaveChanges();
return Redirect("/Account/PersonalInfo");
}
05-28 10:32