gpt4 book ai didi

asp.net - 使用默认的 asp.net mvc 模板时,Html 无法正确插入到电子邮件中

转载 作者:行者123 更新时间:2023-12-02 03:57:24 25 4
gpt4 key购买 nike

使用默认的 mvc 代码来确认注册时的电子邮件地址,但是当电子邮件发送时,它不显示 html。 enter image description here模板MVC代码:

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

异步发送电子邮件:

 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,但我仍然遇到相同的问题

  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 内容。如果 SendEmailAsync 方法仍然以纯文本而不是 HTML 形式发送确认电子邮件,您可以将确认 URL 与正文消息一起嵌入,并让用户的邮件客户端自动将其转换为如下链接:

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

以 HTML 形式发送电子邮件正文的另一种方法是将 SendAsync 方法内的 IsBodyHtml 属性设置为 true,如下所示:

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
}

注意:确保包含 SendAsync 方法的 IdentityConfig.cs 文件在项目的 App_Start 目录中可用(请参阅 this full example )。

相关问题:

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

Email Confirmation with MVC 5 and Asp.net Identity

关于asp.net - 使用默认的 asp.net mvc 模板时,Html 无法正确插入到电子邮件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43356939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com