gpt4 book ai didi

c# - 如何等待 MailMessage.SendAsync?

转载 作者:太空狗 更新时间:2023-10-29 23:05:26 24 4
gpt4 key购买 nike

如果我这样做:

public async Task<SendEmailServiceResponse> ExecuteAsync(SendEmailServiceRequest request)
{
....
var response = new SendEmailServiceResponse();
await client.SendAsync(mail, null); // Has await
response.success = true;
return response;
}

然后我明白了:

Cannot await 'void'

但是如果我这样做:

public async Task<SendEmailServiceResponse> ExecuteAsync(SendEmailServiceRequest request)
{
....
var response = new SendEmailServiceResponse();
client.SendAsync(mail, null); // No Await
response.success = true;
return response;
}

我明白了:

The async method lacks 'await' and will run synchronously.

我显然遗漏了什么,只是不确定是什么。

最佳答案

正如其他人所指出的,SendAsync 有点误导。它返回一个 void,而不是一个 Task。如果你想等待一个发送邮件的调用你需要使用方法

SendMailAsync(MailMessage message)

SendMailAsync(string from, string recipients, string subject, string body)

这两个都返回一个 Task 并且可以等待

关于c# - 如何等待 MailMessage.SendAsync?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43900920/

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