奇怪的异常测试 EmailSender

Strange exception testing EmailSender

我刚刚严格遵循了所有关于在 ASP.NET Core 3.0 MVC 应用程序中构建身份 UI 的 MS 说明。如果我 运行 该项目,它会正常打开默认主页,并且该页面上的链接响应良好。然而,一个简单的集成测试失败并出现异常。

EmailSender 中的单一方法如下所示:

public Task SendEmailAsync(string email, string subject, string htmlMessage)
{
    using (var client = new SmtpClient())
    using (var msg = new MailMessage())
    {
        msg.From = new MailAddress("system@timekeeper.co.za");
        msg.To.Add(new MailAddress(email));
        msg.Subject = subject;
        msg.IsBodyHtml = true;

        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("timekeeper@somewhere.com", "something");
        client.Host = "mail.myhost.com";
        client.Port = 25;
        return client.SendMailAsync(msg);
    }
}

我的测试是这样的:

[TestMethod]
public async Task Send_Email()
{
    var sender = new EmailSender();

    await sender.SendEmailAsync("somebody@somewhere.net", "Test Mail", "This is a test");

    Assert.IsTrue(true);
}

当我 运行 测试失败时出现以下异常:

Test method TimeKeeper.Tests.Integration.EmailSEnderTests.Send_Email threw exception: 
System.Threading.Tasks.TaskCanceledException: A task was canceled.
    at TimeKeeper.Tests.Integration.EmailSEnderTests.Send_Email() in D:\Dev\RestServices\TimeKeeper.Tests.Integration\EmailSEnderTests.cs:line 15
   at Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.ThreadOperations.ExecuteWithAbortSafety(Action action)

await 任务而不是返回它。

public async Task SendEmailAsync(string email, string subject, string htmlMessage) {
    using (var client = new SmtpClient())
    using (var msg = new MailMessage()) {
        msg.From = new MailAddress("system@timekeeper.co.za");
        msg.To.Add(new MailAddress(email));
        msg.Subject = subject;
        msg.IsBodyHtml = true;

        client.UseDefaultCredentials = false;
        client.Credentials = new NetworkCredential("timekeeper@somewhere.com", "something");
        client.Host = "mail.myhost.com";
        client.Port = 25;
        await client.SendMailAsync(msg); //<-- await completion
    }
}

可能 client 在它有机会完成发送电子邮件之前就被处理掉了。

您还应注意文档中有关使用现在 已过时 SmtpClient 进行新开发的警告。

DE0005: SmtpClient shouldn't be used

Motivation

SmtpClient doesn't support many modern protocols. It is compat-only. It's great for one off emails from tools, but doesn't scale to modern requirements of the protocol.

Recommendation

Use MailKit or other libraries.