Search code examples
c#asp.net-coreasp.net-core-webapisendgrid

ASP.NET Core - Send email without wait


I have an APS.NET Core 5 Web API:

I have this:

await _mailSender.SendMailAsync(entity.Id);

But, I don't want to wait for the email sender to return Ok for client. I want to continue code execution and send email in background.

How can I do this?


Solution

  • I did this using HangFire to queue the email to send.

    //send email
    BackgroundJob.Enqueue(() => _mailSender.SendMailAsync(entity.Id));