簡體   English   中英

如何使用asp.net mvc c#向用戶發送電子郵件確認郵件

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

我已經構建了一個應用程序,並希望在注冊后向用戶發送確認電子郵件。我已通過 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; }

}

預期產出

激活成功

實際產量

無效的激活碼

取不同表中的 ActivationCode 並使其在數據庫中成為唯一標識符。現在更新模型中的數據庫並檢查 ActivationCode 屬性是否為 Guid 並執行上述代碼。 結果輸出:激活成功

添加

using System.Runtime.Remoting;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM