我在网站上创建了一个新用户。
用户必须处于事件状态。
我必须向注册的电子邮件地址发送一封激活电子邮件,带有链接以激活帐户。
此链接必须加密,以防被拦截。
当用户单击链接时,系统会激活帐户并登录。
我的 user
表包含:
ID
,
用户名
,
密码
,
ContactInfoID
,
地址ID
,
角色
我的 ContactInformation
表:
ID
,
电子邮件
,
电话
,
单元格
,
传真
,
名称
,
姓氏
user.ContactInfoID
是我的 ContactInformation.ID
字段。
这是我添加用户的方法:
UserEmailCompare ma = new UserEmailCompare();
ma.email = db.ContactInformations.Where(x => x.EMail != null).Select(x => x.EMail);
if (model.Password != model.PasswordCheck)
{
return ErrorResponse("The password you have entered does not match");
}
else
{
if (ma.email.Contains(model.Email))
{
return ErrorResponse("The e-mail address you have entered has already been registered");
}
else
{
if (ModelState.IsValid)
{
User user;
ContactInformation info;
info = new ContactInformation()
{
EMail = model.Email
};
db.ContactInformations.Add(info);
db.SaveChanges();
var only = db.ContactInformations.FirstOrDefault(x => x.EMail == model.Email).ID;
if (model.UserID == 0)
{ //add
user = new User()
{
Username = model.Username,
Password = Globals.CreateHashPassword(model.Password),
ContactInfoID = only
};
db.Users.Add(user);
}
}
}
}
任何人都可以帮助我走得更远。 我有发送电子邮件的类(class)。
最佳答案
首先您需要一个gmail
帐户,使用您喜欢的名称,激活邮件将从该帐户发送
您将需要导入并使用 System.Net.Mail
这是一个示例代码
const string accountName = ""; // # Gmail account name
const string password = ""; // # Gmail account password
MailMessage mail = new MailMessage();
SmtpClient smtp = new SmtpClient("smtp.gmail.com");
smtp.Credentails = new System.Net.NetworkCredential(accountName, password);
mail.From = new MailAddress("youremailaddress@gmail.com"); // # Remember to change here with the mail you got
mail.To.Add(model.Email); // # Email adress to send activation mail
mail.Subject = "Activation Mail";
mail.Body = "Hey there, click this link for activation"; // # You will need to change here with HTML containing a link (which contains a generated activation code)
mail.IsHtml = true;
smtp.Send(mail);
这是关于发送邮件到给定的电子邮件地址。对于整个激活过程,您可以按照以下步骤操作:
- 为包含激活链接和用户信息参数的激活邮件正文创建一个
HTML
模板 - 将
IsActive
和ActivationCode
字段添加到User
表 - 实现激活码生成逻辑或使用
Guid
- 发送邮件成功后,更新
User
表设置发送激活码 - 您需要一个激活页面,它将接收带有查询字符串和激活 用户的生成激活码(将
IsActive
设置为true)
关于c# - 注册用户的激活邮件 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695785/