简体   繁体   English

如何使用asp.net mvc c#向用户发送电子邮件确认邮件

[英]How to send email confirmation mail to user using asp.net mvc c#

I have build an application and want to send user a confirmation email after registration.I have taken ActivationCode value by guid=new.guid();我已经构建了一个应用程序,并希望在注册后向用户发送确认电子邮件。我已通过 guid=new.guid(); 获取了 ActivationCode 值; but it don't get store in the database and after debugging I evaluate the value it says :The name 'ActivationCode' does not exist in the current context.但它不会存储在数据库中,调试后我评估它说的值:当前上下文中不存在名称“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; }

}

Expected Output预期产出

Activation Successful激活成功

Actual Output实际产量

Invalid Activation code无效的激活码

Take ActivationCode in different table and make it uniqueidentifier in database.Now update your database in the model and check for the ActivationCode Property if it is Guid and Execute the above code.取不同表中的 ActivationCode 并使其在数据库中成为唯一标识符。现在更新模型中的数据库并检查 ActivationCode 属性是否为 Guid 并执行上述代码。 Result Output: Activation successfull结果输出:激活成功

添加

using System.Runtime.Remoting;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM