This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service.
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct.

1 Answer
1

Bug

The Main method is ending before the email is sent because we are not waiting for Task response to finish.

SendEmailAsync returns a Task, meaning these two lines of code are identical:

var response = client.SendEmailAsync(sendRequest);

Task response = client.SendEmailAsync(sendRequest);

Solutions

If you have the latest version of Visual Studio installed and have C#7.1 enabled, you can take advantage of async Task Main and use the await keyword which will tell the code to run SendEmailAsync on a different thread and Main won't end until SendEmail has completed.

If you are working on an older version of Visual Studio, you can add .GetAwaiter().GetResult(), which will also ensure Main won't end until SendEmail has completed, but SendEmailAsync will lock the current thread.