I am trying to setup MVCMailer to use the Email and UserId that were setup when they registered with the website. I used the stock MVC framework to setup the user registration framework and thus the ASPNETDB.MDF DB is used to keep track of email userID and password. I am stuck at the moment on how to pull the current user id and email out of the DB so MVCMailer knows where to send the email with the password reset instructions. Below is my attempt at getting the data out of the DB and into the send mail function. I know I am wrong because I have red squiggles below the word membership.
public virtual MailMessage Welcome(string ADID)
{
var mailMessage = new MailMessage{Subject = "Welcome"};
mailMessage.To.Add(membership.Email);
ViewBag.Name = membership.UserId;
PopulateBody(mailMessage, viewName: "Welcome");
return mailMessage;
}
Does anyone have any idea how to do this I have been searching for a couple hours now on how to accomplish this with no luck. The tutorials I have looked at all seem to use hard coded values for where to send an email. Please let me know if you need any other code and Thanks for your help!
You could fetch it by querying your membership provider:
public virtual MailMessage Welcome(string ADID)
{
var mailMessage = new MailMessage{ Subject = "Welcome" };
var user = Membership.GetUser();
mailMessage.To.Add(user.Email);
ViewBag.Name = user.UserName;
PopulateBody(mailMessage, viewName: "Welcome");
return mailMessage;
}
another possibility is to send the email from a controller action and thus pass the information about the currently connected user as parameter:
using Mvc.Mailer;
public class HomeController: Controller
{
private readonly UserMailer _mailer = new UserMailer;
public ActionResult Index()
{
return View();
}
public ActionResult SendEmailToNewUser()
{
var user = Membership.GetUser();
_mailer.Welcome(user.Email, user.UserName).Send();
return View();
}
}