【问题标题】:Html not getting correctly inserted into email when using the default asp.net mvc template使用默认的 asp.net mvc 模板时,HTML 未正确插入电子邮件
【发布时间】: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


【解决方案1】:

问题更可能取决于您当前使用的电子邮件服务/API 内容。如果SendEmailAsync 方法仍然以纯文本而不是 HTML 发送确认电子邮件,您可以将确认 URL 与正文消息一起嵌入,让用户的邮件客户端自动将其转换为如下链接:

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

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

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

【讨论】:

  • 删除锚标签确实有效,但我仍然留下了一个丑陋的链接,可能更干净。此外,正如您在第二个示例中看到的那样,我尝试使用 IsBodyHtml,但仍然遇到同样的问题。
  • 似乎您的第二个代码示例不包含完整的 MailMessage 实例作为 SendMailAsync 方法参数传递,它只传递 Body 属性。如上面给出的完整IdentityConfig.cs 所示,包含IsBodyHtml = trueMailMessage 实例传递到SendMailAsync 以启用HTML 模式作为正文消息。
猜你喜欢
  • 2022-01-24
  • 2016-08-06
  • 2012-12-27
  • 2022-07-29
  • 1970-01-01
  • 2021-05-19
  • 2020-09-11
  • 2015-02-23
  • 1970-01-01
相关资源
最近更新 更多