How to await MailMessage.SendAsync?

asynchronous, c#

Solution

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

SendMailAsync(MailMessage message)

or

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

Both of these return a `Task` and can be awaited

Problem

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 this: Cannot await '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 async method lacks 'await' and will run synchronously. I'm clearly missing something, just not sure what.

Original source