ASP.NET Identity Confirmation Email Is Plain Text Instead of HTML

不羁岁月 提交于 2019-12-23 10:49:33

问题


In an ASP.NET MVC5 C# application I am using the standard code for sending confirmation emails when a user registers, something I've done in multiple projects:

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>");

Normally when the recipient receives the email, it's content-type is text/html. However, in this one application, the emails arrive with content-type text/plain and the content is not rendered as html, it is rendered as plain text. It should like this:

Please confirm your account by clicking here

But it looks like this:

Please confirm your account by clicking <a href="https://localhost:44309/Account/ConfirmEmail?userId=8b126243-1f78-4e42-87d2-ab3c7c350f99&code=tokengoeshere">here</a>

I searched the docs but cannot find any information that indicates why this would be or indeed how to change this behavior.

I also updated to the latest version of the Microsoft ASP.NET Identity Core (2.2.1) thinking that might help, but it didn't have any effect on the issue.

I also tested sending from a Google Apps email account for a different organization (which I use for other apps with this exact same code to send this confirmation email and know it sends as html) in case there are some settings in there that matter.

Any ideas on why it sends as text/plain and how I can fix this?

Thanks!


回答1:


The issue is likely with the mail service/api you're using. I have used several and they generally have either an optional htmlBody parameter or an isBodyHtml parameter. If you specify which one you're using, or provide the code for the SendAsync Methos in the EmailService class (in IdentityConfig.cs), it'll probably be easy to point you in the right direction.

If your stuck with plain text, you can send plain text with a url and the client will often convert it into a link for the user. Outlook and gmail do this.

So, it would look like this instead:

await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account:" + callbackUrl);



回答2:


The problem more than likely isn't with your Identity, it is probably just with the mail function itself. Have you set the MailMessage.IsBodyHtml property to true?

MailMessage message = new MailMessage(fromEmail, toEmail, subject, body);
message.IsBodyHtml = true; // here

This will set the body of the email to be rendered as HTML. By default, the property is false, so you need to explicitly declare it to be true.




回答3:


You can use SendGrid API to send HTML type emails and they also provide transactional templates.

Links

https://sendgrid.com/

Even advised in the Asp.net core doc

https://docs.microsoft.com/en-us/aspnet/core/security/authentication/accconfirm?view=aspnetcore-2.2&tabs=visual-studio



来源:https://stackoverflow.com/questions/32192836/asp-net-identity-confirmation-email-is-plain-text-instead-of-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!