c# - 注册用户的激活邮件 MVC

标签 c# asp.net-mvc email encryption smtp

我在网站上创建了一个新用户。 用户必须处于事件状态。 我必须向注册的电子邮件地址发送一封激活电子邮件,带有链接以激活帐户。 此链接必须加密,以防被拦截。 当用户单击链接时,系统会激活帐户并登录。 我的 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 模板
  • IsActiveActivationCode 字段添加到User
  • 实现激活码生成逻辑或使用Guid
  • 发送邮件成功后,更新User表设置发送激活码
  • 您需要一个激活页面,它将接收带有查询字符串和激活 用户的生成激活码(将IsActive 设置为true)

关于c# - 注册用户的激活邮件 MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21695785/

相关文章:

asp.net-mvc - DataAnnotation 正则表达式对于文件输入总是返回 false

javascript - 直接在邀请邮件中打开应用商店

c# - 在具有 Entity Framework 奇怪行为的循环中构建查询

c# - 隐藏 TabControl header

asp.net-mvc - MVC4 DataType.Date EditorFor 不会在 Chrome 中显示日期值,在 Internet Explorer 中很好

mysql - 我们可以基于MYSQL数据库构建ASP.NET MVC吗?

asp.net - 在 Asp.Net 中群发 HTML 通讯

email - Magento 如何投递邮件?

c# - 尽管将 ContentPlaceHolder 和 <div> 设置为宽度 :100%,但内容超出了屏幕

c# - 有没有办法获得Redis key 的空闲时间