title: Async method returns 500 Internal Server Error to view, even after it completes
body: <p>I have a method for sending emails, which makes use of <code>SmtpClient</code>. At one point in this method, I have the following <code>await smtpClient.SendMailAsync (mail);</code>. The call is made from my controller as follows, depending on the case:</p>
  
  <pre><code>    [HttpPost]
      [AllowAnonymous]
      [ValidateJsonAntiForgeryToken]
      public async Task&lt;ActionResult&gt; RegisterParticipant(RegisterIndexBaseVm attendee)
      {
          if (attendee == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          if (ModelState.IsValid)
          {
              attendee.EventId = Int32.Parse(Session["id"].ToString());
              using (var client = new HttpClient())
              {
                  client.BaseAddress = new Uri(BaseUrl);
                  client.DefaultRequestHeaders.Clear();
                  client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                  HttpResponseMessage Res = await client.PostAsync("Register/RegisterAttendee", new StringContent(JsonConvert.SerializeObject(Mapper.ToParticipant(attendee)), Encoding.UTF8, "application/json"));
                  if (Res.IsSuccessStatusCode)
                  {
                      var EmpResponse = Res.Content.ReadAsStringAsync().Result;
                      var result = JsonConvert.DeserializeObject&lt;EmailTemplateDTO&gt;(EmpResponse);
                      if (result.NotError == false)return Json(new { success = false, message = Constants.CulqiResponses[result.Response] }, JsonRequestBehavior.AllowGet);
                      if (string.IsNullOrEmpty(result.InvoiceInstitution))
                      {
  
                          if (result.SendEmail == true) MailHelper.SendMail(Enumerables.MailAction.EventRegisterSuccess, result);
                          return Json(new { success = true }, JsonRequestBehavior.AllowGet);
                      }
                      else
                      {
                          if (result.SendEmail == true) MailHelper.SendMail(Enumerables.MailAction.EventRegisterSuccessWithInvoice, result);
                          return Json(new { success = true, message = "Registro Exitoso" }, JsonRequestBehavior.AllowGet);
                      } 
                  }
                  else
                  {
                      return Json(new { success = false }, JsonRequestBehavior.AllowGet);
                  }
              }
          }
          return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
      }
  </code></pre>
  
  <p>In the web application, I have an AJAX that executes the call to said controller, which uses the parameter <code>async: true</code>, but I always get a 500 error. I do not understand the reason well and how I should make the correct call to each method and class to get the correct results.</p>
  
  <pre><code>$.ajax({
      type: 'POST',
      url: url,
      headers: headers,
      dataType: 'json',
      async: true,
      data: { attendee: enviar },
      success: function (response) {
          $('#payment').modal('hide');
          if (payType === 'Culqui') {
              resultdiv("", 'success');
          }
          else {
              resultdiv("", 'warning');
          }
  
      },
      error: function (result) {
          $('#payment').modal('hide')
          resultdiv("", 'error');
      }
  });
  </code></pre>
  
  <p>The answer is the following:</p>
  
  <p><code>An asynchronous module or handler completed while an asynchronous operation was still pending.</code></p>
  
  <p>But the sending of the emails is done with no inconvenience. It is necessary that the method of sending emails has the following <code>public static async void SendMail</code>, I do not expect any real response from the same from the controller.</p>