How to wait for MailMessage.SendAsync?

If I do this:

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

Then I get the following:

I can not wait for "void"

But if I do this:

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

I get this:

The asynchronous method has no "wait" and will be executed synchronously.

Something is obviously missing me, I just don’t know what.

+5
source share
1 answer

As others have noted, SendAsync bit misleading. It returns void , not Task . If you want await to send a mail call, you need to use the method

 SendMailAsync(MailMessage message) 

or

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

Both of them return a Task and can be expected.

+5
source

All Articles