RedirectToAction after callback in ASP.NET MVC | redirecttoaction
Dontuseacallback.RedirectToActioncreatesanActionResultthatshouldbereturnedbytheaction,itdoesntforcearedirect.Theproperwaytodosomethingasynchronouslyistouseasync/await.Evenifyouremaillibrarydoesnthavetask-basedasynchronousmethodsyoucanadaptittothetask-basedmodelusingaTaskCompletionSource.Thatwouldberatherunusualthoughasmostlibrarieshavemovedfromolderasyncmodelslikecallbacks,eventsandAPMtotasks.MailMessagesuggestsyoureusingSmtpClient[1].TheSendMailAsync[2]methodistask-based,whichmeansyoucanwri...
Dont use a callback. RedirectToAction creates an ActionResult that should be returned by the action, it doesnt force a redirect.
The proper way to do something asynchronously is to use async/await. Even if your email library doesnt have task-based asynchronous methods you can adapt it to the task-based model using a TaskCompletionSource. That would be rather unusual though as most libraries have moved from older async models like callbacks, events and APM to tasks.
MailMessage suggests youre using SmtpClient[1]. The SendMailAsync[2] method is task-based, which means you can write
await client.SendMailAsync(email);For example :
[HttpPost] public async Task<ActionResult> SendEmail(EmailContentViewModel emailDetails) { SmptClient client = ... //Configure the client here using (MailMessage email = new MailMessage(emailDetails.from, emailDetails.to)) { email.Subject = emailDetails.subject; email.Body = emailDetails.body...