Last post Dec 17, 2018 06:14 PM by Xequence
Dec 16, 2018 11:26 PM|CarCancelo|LINK
A little project I am working on, need to send various different forms from the site. I need the forms to have the "From" address of the current logged in user. I am using Identity 2.0 for authentication and the standard "System.Net.Mail" namespace.
In the controller I have:
var message = new MailMessage();
message.To.Add(new MailAddress("email addresses")); // these should also be variables coming from the database
message.From = new MailAddress("current user email"); // set to the identity 2.0 current users email address.
I can use:
ApplicationUser user = UserManager.FindByName(userName);
string mail= user.Email;
to get the email address but now I'm kinda stuck on how to use that as a variable for the email form.
Thanks. Any help would be appreciated!!
Dec 17, 2018 01:37 PM|CarCancelo|LINK
Really simple solution, in case anyone else has this question.
At the controller method add:
string userId = User.Identity.GetUserName();
then use the "userID" for the variable:
message.From = new MailAddress(userID); // set to the identity 2.0 current users email address.
Dec 17, 2018 01:45 PM|PatriceSc|LINK
Not sure to see what is the problem in reusing the variable ie :
The problem is with the To address ? Which data do you have for this user, it's User ID ? IMO don't use the UserName. For the default templates it is set to the mail address but this is not the general case. Do that only if this your current approach as
Dec 17, 2018 06:14 PM|Xequence|LINK
You want to return a View or Partial View with your model. Requires a strongly typed view model though.
Example: var model = EmailObject();
model.Email = user.Email;