简体   繁体   English

Asp.Net Identity 2.0 - 如何使用SmtpClient实现IIdentityMessageService来执行异步SMTP?

[英]Asp.Net Identity 2.0 - How to Implement IIdentityMessageService to do Async SMTP using SmtpClient?

I've implemented a simple EmailService for Asp.Net Identity 2.0 (via the IIdentityMessageService interface. 我实现了Asp.Net 2.0身份简单EmailService(通过IIdentityMessageService接口。

    public class EmailService : IIdentityMessageService
{
    public Task SendAsync(IdentityMessage message)
    {
        // convert IdentityMessage to a MailMessage
        var email = 
           new MailMessage(new MailAddress("noreply@mydomain.com", "(do not reply)"), 
           new MailAddress(message.Destination))
        {
            Subject = message.Subject,
            Body = message.Body,
            IsBodyHtml = true
        };

        using (var client = new SmtpClient()) // SmtpClient configuration comes from config file
        {
            return client.SendMailAsync(email);
        }
    }
}

To send an email, I go through UserManager: 要发送电子邮件,我将通过UserManager:

await _userManager.SendEmailAsync(user.Id, "Confirm your account","Please confirm your account by clicking this link: <a href=\\"" + callbackUrl + "\\">link</a>");

The problem is that I get a System.Threading.Tasks.TaskCanceledException when I call SendEmailAsync() and it's not clear why. 问题是,当我调用SendEmailAsync()时,我得到一个System.Threading.Tasks.TaskCanceledException ,并且不清楚为什么。

If I send email synchronously ( client.Send(email) ), everything works fine. 如果我同步发送电子邮件( client.Send(email) ),一切正常。

So my questions are: 所以我的问题是:

  • How do I prevent the TaskCanceledException from getting thrown? 如何防止TaskCanceledException被抛出? and (assuming I can overcome this error), (假设我能克服这个错误),

  • How should I go about communicating errors during email sending back to the client (ie, "no such user here" type responses from the SmtpClient? 我应该如何在电子邮件发送回客户端时传达错误(即“此处没有此类用户”类型的响应来自SmtpClient?

Your problem is that SmtpClient is disposed before the email is sent. 您的问题是在发送电子邮件之前处理了SmtpClient

Two ways: 两种方式:

  • Await the SendMailAsync result 等待SendMailAsync结果

     using (var client = new SmtpClient()) { await client.SendMailAsync(email); } 
  • Register the SendCompleted event and dispose the SmtpClient only after the message is sent 注册SendCompleted事件并仅在发送消息后处置SmtpClient

     var client = new SmtpClient(); client.SendCompleted += (s, e) => { client.Dispose(); }; return client.SendMailAsync(message); 

You must put async on method. 您必须在方法上放置异步。

    public async Task SendAsync(IdentityMessage message)
    {
        using (SmtpClient client = new SmtpClient())
        {
                using (var mailMessage = new MailMessage("your@email.com", message.Destination, message.Subject, message.Body))
                {
                    await client.SendMailAsync(mailMessage);
                }
            }
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.Net IIdentityMessageService 中使用 SMTP 或 Office 365 帐户? - How to use SMTP or Office 365 account in ASP.Net IIdentityMessageService? ASP.NET Identity 3.0上IIdentityMessageService的等价物是什么? - What is the equivalent for IIdentityMessageService on ASP.NET Identity 3.0? ASP.NET Identity UserManager IIdentityMessageService将额外参数传递给SendAsync - ASP.NET Identity UserManager IIdentityMessageService pass extra parameters to SendAsync 如何在现有数据库中实现ASP.NET Identity 2.0? - How to implement ASP.NET Identity 2.0 in existing database? 如何在Asp.net MVC中使用Identity 2.0实现基于声明的身份验证 - How to implement claims-based authentication using Identity 2.0 in Asp.net MVC 我们如何在 ASP.NET 身份中实现权限? - How do we implement permissions in ASP.NET identity? 如何使用ASP.NET Identity for ASP.NET MVC 5.0实现密码重置? - How do I implement password reset with ASP.NET Identity for ASP.NET MVC 5.0? 如何使用smtpClient与ASP.Net安全地发送我的凭证 - How to send my Credentials securely using smtpClient with ASP.Net 如何使用Asp.Net.Identity和Entity Framework 6在Asp.Net Core 2.0 Target Framework 4.6.1中设置身份? - How can I setup identity in Asp.Net Core 2.0 Target Framework 4.6.1 using Asp.Net.Identity and Entity Framework 6? 如何使用ASP.NET Identity 2.0允许用户模拟其他用户? - How do I use ASP.NET Identity 2.0 to allow a user to impersonate another user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM