Reputation: 1511
Using the default mvc code for confirming email addresses on registration but when the email gets sent it isn't showing the html. Template MVC code:
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" +
callbackUrl + "\">here</a>");
Send Email Async:
public Task SendAsync(IdentityMessage message)
{
// Plug in your email service here to send an email.
SmtpClient client = new SmtpClient();
return client.SendMailAsync("email here",
message.Destination,
message.Subject,
message.Body);
return Task.FromResult(0);
}
When looking through some of the other questions on SO, I changed my send code slightly so that I could set the body to allow html but I still have the same issue
public Task SendAsync(IdentityMessage message)
{
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.Body = message.Body;
// Plug in your email service here to send an email.
SmtpClient client = new SmtpClient();
return client.SendMailAsync("email here",
message.Destination,
message.Subject,
msg.Body);
return Task.FromResult(0);
}
Any ideas as to why this is happening?
TIA
Upvotes: 2
Views: 2185
Reputation: 24957
The problem more likely depends on which email service/API content you've currently using. In case SendEmailAsync
method still sending confirmation email as plain text instead of HTML, you can embed the confirmation URL together with body message and let user's mail client automatically convert it to a link like this:
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here: " + callbackUrl);
Another way to send email body as HTML is setting IsBodyHtml
property to true
inside SendAsync
method as shown below:
public class EmailService : IIdentityMessageService
{
// other stuff
public async Task SendAsync(IdentityMessage message)
{
var msg = new MailMessage();
msg.Subject = message.Subject;
msg.Body = message.Body;
msg.IsBodyHtml = true;
msg.To.Add(message.Destination);
// Plug in your email service here to send an email.
using (var client = new SmtpClient())
{
await client.SendMailAsync(msg);
}
// other stuff
}
// other stuff
}
NB: Ensure the IdentityConfig.cs
file containing SendAsync
method is available in App_Start
directory of your project (see this full example).
Related issues:
ASP.NET Identity Confirmation Email Is Plain Text Instead of HTML
Email Confirmation with MVC 5 and Asp.net Identity
Upvotes: 6