Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用默认asp.net mvc模板时,Html未正确插入到电子邮件中_Asp.net_Asp.net Mvc_Email - Fatal编程技术网

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

使用默认asp.net mvc模板时,Html未正确插入到电子邮件中,asp.net,asp.net-mvc,email,Asp.net,Asp.net Mvc,Email,使用默认mvc代码确认注册时的电子邮件地址,但当电子邮件发送时,它不会显示html。 模板MVC代码: string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol:

使用默认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>");
在查看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内容。如果
sendmailasync
方法仍以纯文本而非HTML形式发送确认电子邮件,则可以将确认URL与正文消息一起嵌入,并让用户的邮件客户端自动将其转换为如下链接:

await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking here: " + callbackUrl);
另一种以HTML形式发送电子邮件正文的方法是将
IsBodyHtml
属性设置为
true
inside
SendAsync
方法,如下所示:

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
目录中可用(请参阅)

相关问题:


如果您不确定锚定链接,可以将令牌URL传递到正文中,如下所示:
等待UserManager.SendEmailAsync(user.Id,“确认您的帐户”,“请单击此处确认您的帐户:“+callbackUrl”)
。在上一个代码示例中,尝试使用
return client.SendMailAsync(msg)
而不是使用旧模式。要添加到Tetsuya的答案中,您可能还需要设置“发件人”电子邮件,如msg.from=新邮件地址(“email@domain.com");移除锚定标签确实有效,但我仍然有一个丑陋的链接,可以更干净。另外,正如您在第二个示例中看到的,我尝试使用IsBodyHtml,但仍然存在相同的问题。似乎您的第二个代码示例没有包含要作为
SendMailAsync
方法参数传递的完整
MailMessage
实例,它只传递
Body
属性。如上面给出的complete
IdentityConfig.cs
所示,
MailMessage
实例,其中包括传递到
SendMailAsync
中的
IsBodyHtml=true
以启用HTML模式作为正文消息。
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
}