gpt4 book ai didi

asp.net - 奇怪的异常测试 EmailSender

转载 作者:行者123 更新时间:2023-11-28 20:48:49 25 4
gpt4 key购买 nike

我刚刚严格遵循了所有关于在 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)

最佳答案

等待任务而不是返回它。

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
}
}

客户端 有可能在它有机会完成发送电子邮件之前被处理掉。

您还应该注意文档中有关使用现在已过时 的警告 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'tscale to modern requirements of the protocol.

Recommendation

Use MailKit or other libraries.

关于asp.net - 奇怪的异常测试 EmailSender,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55263527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com