I have made a basic application where users can sign up using asp.net core identity framework.
When a user signs up they are sent a confirmation code which they click to confirm their account and sign in.
The problem is I don't want the big long randomly generated link in the email, I am trying to pass the link in an "" tag with some html. Unfortunately the email completely ignores the html formatting and sends everything through as one big long string.
I feel like I have tried every different combination so am open to any suggestions
here is an example of one of the emails I get sent
Please confirm your account by clicking this link: <a
href='https://localhost:44380/Account/ConfirmEmail?userId=02678522-9cca-
41a3-b6ad5ffbc66335d0&token=CfDJ8JiT8fifBtNEgqHa2cUXO7tOkDv6hjChQMu4I7j99p0B
nc2UEc8wV6QnL2lQPau%2BmdoGBPK3ic4ocy2M6K4bIrJnzvFmGFVxqOnKx3nZn3896SuUO1dpR
L7DHahnHP3Wy5JZlyFJrkJ5ncnF%2BUXpJoBzS%2FupjkJ4kv5SUxwQ0V3
ytb7jKdv0pEs7mtEfcDlO%2B8EcdJdEJMQePfwf%2FPr73IrOrSU499WLon0kFfzN0qg
jQOe0lI7yuDZv1aam udb1g%3D%3D'>Confirm</a>
EmailData
public class EmailData
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly EFCoreOrganisationDb _context;
public EmailData(UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager, EFCoreOrganisationDb
context)
{
_userManager = userManager;
_signInManager = signInManager;
_context = context;
}
public static void SendAuth(string userName, string userEmail, string
cTokenLink)
{
//this didnt work
//string body = @"<html><body><a href='+cTokenLink+'>here</a>
</body></html>";
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = String.Format("Click here {0} to confirm
your email address", cTokenLink);
string body = bodyBuilder.HtmlBody;
string SenderName = "BlabberApp";
var message = new MimeMessage();
message.From.Add(new MailboxAddress(SenderName, "************"));
message.To.Add(new MailboxAddress(userName, userEmail));
message.Subject = "Email Confirmation From Blabber";
message.Body = new TextPart("plain")
{
Text = body
};
using (var client = new SmtpClient())
{
client.Connect("smtp.gmail.com", 587, false);
client.Authenticate("************", "**********");
client.Send(message);
client.Disconnect(true);
}
}
}
AccountController
public class AccountController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;
private readonly EFCoreOrganisationDb _context;
public AccountController(UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager, EFCoreOrganisationDb
context)
{
_userManager = userManager;
_signInManager = signInManager;
_context = context;
}
[HttpGet]
public IActionResult Login()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel vm)
{
if (ModelState.IsValid)
{
var result = await
_signInManager.PasswordSignInAsync(vm.Email, vm.Password, vm.RememberMe,
false);
if (result.Succeeded)
{
return View("../Home/Index");
}
var error = result.ToString();
if (error.Count() > 1)
{
ModelState.AddModelError("", "Invalid Login Attempt" );
}
return View(vm);
}
return View(vm);
}
[HttpPost]
public async Task<IActionResult> Logout()
{
await _signInManager.SignOutAsync();
return RedirectToAction("Index", "Home");
}
[HttpGet]
public IActionResult Register()
{
return View();
}
[HttpPost]
public async Task<IActionResult> Register(RegisterViewModel vm)
{
try
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = vm.Email,
Email = vm.Email };
var result = await _userManager.CreateAsync(user,
vm.Password);
await _userManager.AddToRoleAsync(user, "User");
if (result.Succeeded)
{
string cToken = await
_userManager.GenerateEmailConfirmationTokenAsync(user);
string cTokenLink = Url.Action("ConfirmEmail",
"Account", new
{
userId = user.Id,
token = cToken
},protocol:HttpContext.Request.Scheme);
EmailData.SendAuth(user.UserName,user.Email,cTokenLink);
//ViewBag.token = cTokenLink;
await _signInManager.SignInAsync(user, false);
return View(vm);
}
else
{
foreach (var error in result.Errors)
{
ModelState.AddModelError("", error.Description);
}
}
}
}
catch(Exception ex)
{
}
return View(vm);
}
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> ConfirmEmail(string userId, string
token)
{
if(userId == null || token == null)
{
return View("Error");
}
var user = await _userManager.FindByIdAsync(userId);
if(user == null)
{
return View("Error");
}
var result = await _userManager.ConfirmEmailAsync(user, token);
return View(result.Succeeded ? "ConfirmEmail" : "Error");
}
}
I was hoping this would send the email with the one sentence and a clickable link.
Instead it sends the raw html and the super long link inside of it
Changing from TextPart("plain") to TextPart("html") in my EmailData script fixed the problem.
Thanks to @Chris Pratt for pointing that out to me