【发布时间】:2017-09-07 11:46:37
【问题描述】:
在注册时使用默认的 mvc 代码来确认电子邮件地址,但是当电子邮件被发送时,它没有显示 html。 模板 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
【问题讨论】:
-
如果您不确定锚链接,可以将令牌 URL 传递到正文中,如下所示:
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here: " + callbackUrl)。在最后一个代码示例中,尝试使用return client.SendMailAsync(msg)而不是使用旧模式。 -
要添加到 Tetsuya 的答案,您可能还需要设置“发件人”电子邮件,例如 msg.From = new MailAddress("email@domain.com");
标签: asp.net asp.net-mvc email