如何使用 asp.net mvc c# 向用户发送电子邮件确认邮件
How to send email confirmation mail to user using asp.net mvc c#
我已经构建了一个应用程序,并希望在 registration.I 通过 guid=new.guid() 获取 ActivationCode 值后向用户发送一封确认电子邮件;但它没有存储在数据库中,调试后我评估了它说的值:名称 'ActivationCode' 在当前上下文中不存在。
public ActionResult Activation()
{
ViewBag.Message = "Invalid Activation code.";//This get outputted always
if (RouteData.Values["id"] != null)
{
Guid activationcode = new Guid(RouteData.Values["id"].ToString());
IPHISEntities usersEntities = new IPHISEntities();
User userActivation = usersEntities.Users.Where(p => p.ActivationCode == activationcode).FirstOrDefault();
if (userActivation != null)//this condition gets true
{
usersEntities.Users.Remove(userActivation);
try {
usersEntities.SaveChanges();
}
catch (DbEntityValidationException e)
{
Console.WriteLine(e);
}
ViewBag.Message = "Activation successful.";
}
}
return View();
}
private void SendActivationEmail(User user)
{
Guid activationcode = Guid.NewGuid();
IPHISEntities usersEntities = new IPHISEntities();
usersEntities.Users.Add(new User
{
UserId = user.UserId,
ActivationCode = activationcode
});
try
{
usersEntities.SaveChanges();
}
catch (DbEntityValidationException e)
{
Console.WriteLine(e);
}
using (MailMessage mm = new MailMessage("xyzz@gmail.com", user.EmailAddress))
{
mm.Subject = "Account Activation";
string body = "Hello " + user.FirstName + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + string.Format("{0}://{1}/User/Activation/{2}", Request.Url.Scheme, Request.Url.Authority, activationcode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("xyzz@gmail.com", "********");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
//**User.cs**:
namespace ConnectionSQL_webAPI_.Models
{
using System;
using System.Collections.Generic;
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.Answers = new HashSet<Answer>();
this.Appointments = new HashSet<Appointment>();
this.DoctorInfoes = new HashSet<DoctorInfo>();
this.Payments = new HashSet<Payment>();
this.UserLoginDetails = new HashSet<UserLoginDetail>();
}
public int UserId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public System.Guid ActivationCode { get; set; }
public string Password { get; set; }
public string PhoneNumber { get; set; }
public string UserUniqueId { get; set; }
public Nullable<int> UserTypeId { get; set; }
public Nullable<int> AddressId { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
}
预期输出
Activation Successful
实际输出
Invalid Activation code
添加
using System.Runtime.Remoting;
在不同的 table 中获取 ActivationCode 并使其成为 database.Now 中的 uniqueidentifier 更新模型中的数据库并检查 ActivationCode 属性 如果它是 Guid 并执行上面的代码。
结果输出:激活成功
我已经构建了一个应用程序,并希望在 registration.I 通过 guid=new.guid() 获取 ActivationCode 值后向用户发送一封确认电子邮件;但它没有存储在数据库中,调试后我评估了它说的值:名称 'ActivationCode' 在当前上下文中不存在。
public ActionResult Activation()
{
ViewBag.Message = "Invalid Activation code.";//This get outputted always
if (RouteData.Values["id"] != null)
{
Guid activationcode = new Guid(RouteData.Values["id"].ToString());
IPHISEntities usersEntities = new IPHISEntities();
User userActivation = usersEntities.Users.Where(p => p.ActivationCode == activationcode).FirstOrDefault();
if (userActivation != null)//this condition gets true
{
usersEntities.Users.Remove(userActivation);
try {
usersEntities.SaveChanges();
}
catch (DbEntityValidationException e)
{
Console.WriteLine(e);
}
ViewBag.Message = "Activation successful.";
}
}
return View();
}
private void SendActivationEmail(User user)
{
Guid activationcode = Guid.NewGuid();
IPHISEntities usersEntities = new IPHISEntities();
usersEntities.Users.Add(new User
{
UserId = user.UserId,
ActivationCode = activationcode
});
try
{
usersEntities.SaveChanges();
}
catch (DbEntityValidationException e)
{
Console.WriteLine(e);
}
using (MailMessage mm = new MailMessage("xyzz@gmail.com", user.EmailAddress))
{
mm.Subject = "Account Activation";
string body = "Hello " + user.FirstName + ",";
body += "<br /><br />Please click the following link to activate your account";
body += "<br /><a href = '" + string.Format("{0}://{1}/User/Activation/{2}", Request.Url.Scheme, Request.Url.Authority, activationcode) + "'>Click here to activate your account.</a>";
body += "<br /><br />Thanks";
mm.Body = body;
mm.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential NetworkCred = new NetworkCredential("xyzz@gmail.com", "********");
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
}
//**User.cs**:
namespace ConnectionSQL_webAPI_.Models
{
using System;
using System.Collections.Generic;
public partial class User
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public User()
{
this.Answers = new HashSet<Answer>();
this.Appointments = new HashSet<Appointment>();
this.DoctorInfoes = new HashSet<DoctorInfo>();
this.Payments = new HashSet<Payment>();
this.UserLoginDetails = new HashSet<UserLoginDetail>();
}
public int UserId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public System.Guid ActivationCode { get; set; }
public string Password { get; set; }
public string PhoneNumber { get; set; }
public string UserUniqueId { get; set; }
public Nullable<int> UserTypeId { get; set; }
public Nullable<int> AddressId { get; set; }
public Nullable<System.DateTime> CreatedDate { get; set; }
}
预期输出
Activation Successful
实际输出
Invalid Activation code
添加
using System.Runtime.Remoting;
在不同的 table 中获取 ActivationCode 并使其成为 database.Now 中的 uniqueidentifier 更新模型中的数据库并检查 ActivationCode 属性 如果它是 Guid 并执行上面的代码。 结果输出:激活成功