sco*_*ker 2 asp.net email asp.net-mvc
使用默认的 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>");
Run Code Online (Sandbox Code Playgroud)
发送电子邮件异步:
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);
}
Run Code Online (Sandbox Code Playgroud)
在查看有关 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);
}
Run Code Online (Sandbox Code Playgroud)
关于为什么会发生这种情况的任何想法?
TIA
问题更可能取决于您当前使用的电子邮件服务/API 内容。如果SendEmailAsync方法仍然以纯文本而不是 HTML 的形式发送确认电子邮件,您可以将确认 URL 与正文消息一起嵌入,并让用户的邮件客户端自动将其转换为这样的链接:
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here: " + callbackUrl);
Run Code Online (Sandbox Code Playgroud)
将电子邮件正文作为 HTML 发送的另一种方法是将IsBodyHtml属性设置为trueinsideSendAsync方法,如下所示:
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
}
Run Code Online (Sandbox Code Playgroud)
注意:确保IdentityConfig.cs包含SendAsync方法的文件在App_Start您的项目目录中可用(请参阅此完整示例)。
相关问题:
使用 MVC 5 和 Asp.net Identity 进行电子邮件确认
| 归档时间: |
|
| 查看次数: |
1449 次 |
| 最近记录: |