I read the documentation for async-await
feature, but still highly confused on how to structure async-await
code that calls SmtpClient.SendMailAsync()
method.
How should I rewrite the code to properly use async-await
?
Currently, my code looks like this:
Periodically PrivateSignal
is being called by the system to send out email notifications.
public override void PrivateSignal(IEventInformation ev)
{
TagEvent tag = (TagEvent)ev;
Task.Run(async () =>
{
try
{
await smptClient.SendMailAsync(CaptureRC.SmptFromEmailAddr,
ToEmails,
CaptureRC.EmailSubject,
"seen moving" + tag.ToString());
}
catch (AggregateException ex)
{
//TODO handle the error
//TODO log the erros, along with
}
}).Wait();
}