Im trying to send multiple email on item approved in sitecore workflows, but i think the workflows doesnt wait on the async method to complete, it's run through the send mail function and out of workflows even if i await. I have used the same method on publish end event, and it's worked perfectly. How can i make the workflows wait on the async method ?
Here is my workflows event code(the SendMailToUser method go through a couple process mail method and eventually to the sendmailasync method :
public async Task Process(WorkflowPipelineArgs args)
{
Assert.ArgumentNotNull(args, "args");
Item item = args.DataItem;
if (item != null)
{
await SendMailToUser(item);
}
else
{
Log.Info(String.Format("Item is null or not found"), this);
}
}
And here is my send mail function :
public async Task SendMail(MailModel mail, SMTPConfig config)
{
...
await smtpClient.SendMailAsync(message);
Log.Info("Mail sent",this);
}
public async Task SendMailAsync(MailModel mail, SMTPConfig config, List<string> listRecipent)
{
try
{
for (int i = 0; i < listRecipent.Count; i++)
{
mail.to = null;
mail.to = new MailAddress(listRecipent[i]);
await SendMail(mail, config);
}
}
catch (ProtocolException ex)
{
Sitecore.Foundation.SitecoreExtensions.Extensions.StringExtensions.LogError(ex.ToString(), "SMTPService.SendMail");
}
}