Html not getting correctly inserted into email when using the default asp.net mvc template
使用默认的mvc代码在注册时确认电子邮件地址,但是发送电子邮件时不会显示html。
模板MVC代码:
1 2 3 4 | 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"); |
发送电子邮件异步:
1 2 3 4 5 6 7 8 9 10 11 | 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); } |
浏览SO上的其他一些问题时,我略微更改了发送代码,以便可以将正文设置为允许html,但是我仍然遇到相同的问题
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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); } |
为什么会这样?
TIA
该问题更有可能取决于您当前使用的电子邮件服务/ API内容。如果
1 | await UserManager.SendEmailAsync(user.Id,"Confirm your account","Please confirm your account by clicking here:" + callbackUrl); |
发送电子邮件正文为HTML的另一种方法是在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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 } |
注意:确保包含
相关问题:
ASP.NET身份确认电子邮件是纯文本而不是HTML
具有MVC 5和Asp.net身份的电子邮件确认