2

Using the default mvc code for confirming email addresses on registration but when the email gets sent it isn't showing the html. enter image description here 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

2
  • If you're not sure about anchor link, you can pass the token URL into the body like this: await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here: " + callbackUrl). In the last code sample, try using return client.SendMailAsync(msg) instead of using old pattern. Commented Apr 12, 2017 at 9:27
  • To add to Tetsuya's answer, you probably also need to set the "from" email like msg.From = new MailAddress("[email protected]"); Commented Feb 3, 2021 at 16:52

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Removing the anchor tags does work but then I'm still left with an ugly link that could be cleaner. Also, as you can see by the second example, I've tried using the IsBodyHtml and I still had the same issue.
Seems that your second code sample doesn't include complete MailMessage instance to be passed as SendMailAsync method argument, it only passing Body property instead. As shown in complete IdentityConfig.cs given above, MailMessage instance which includes IsBodyHtml = true passed into SendMailAsync to enable HTML mode as body message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.