title: Using Asp.Net to SendAsync emails but Page still waits?
body: <p>I don't want the user to wait for page completing the sending processes, so I'm thought of using SendAsync in ASP.NET 3.5. But for after debuging, I found that the Main Thread <b>still</b> waits. </p>
  
  <pre>
  Main: Call send email function...
  mailSend: Configuring....
  mailSend: setting up incorrect port....
  mailSend: Attempt now to send....
  mailSend: End of Line
  Main: Email Function call finish.
  Main: Proccess Complete!
  mailComp: Sending of mail failed, will try again in 5 seconds...
  mailComp: Retrying...
  mailComp: Send successful!
  mailComp: End of Line
  </pre>
  
  <p>Now the I placed incorrect port setting so the first email fails, and test to see if the second time it will be successful. Even with the correct port, the page still waits. Only after mailComp function is finish is when the Page is finally posted. Is this some limitation to SendAsyn? </p>
  
  <p>And here some code, not sure if this will be helpful.</p>
  
  <pre>
  
      protected void btnReset_Click(object sender, EventArgs e)
      {
          try
          {
              DataContext db = new DataContext();
  
              var query = from u in db.Fish
                          where u.Username == txtUsername.Text & u.Email == txtEmail.Text
                          select new { u.Username, u.Email };
  
              if (query.Count() != 0)
              {
  
  
                  User user = new User();
                  String token = user.requestPasswordReset(txtUsername.Text);
                  String URL = Request.Url.AbsoluteUri.ToString() + "?token=" + token;
                  String body = "Reseting you password if you \n" + URL + "\n \n ";
  
  
                  Untilty.SendEmail(txtEmail.Text, "Reseting Password", body);
  
                  litTitle.Text = "Message was sent!";
                  litInfo.Text = "Please check your email in a few minuets for further instruction.";
                  viewMode(false, false);
              }
              else
              {
                  litCannotFindUserWithEmail.Visible = true;
              }
  
  
          }
          catch (Exception ex)
          {
              Debug.Write("Main: Exception: " + ex.ToString());
              litInfo.Text = "We are currently having some technically difficulty. Please try  again in a few minuets. If you are still having issue call us at 905344525";
          }
      }
  
  
  
  ///
  public static class Utility
  
  ///
  
      public static void SendEmail(string recipient, string subject, string body)
      {
          MailMessage message = new MailMessage();
  
          message.To.Add(new MailAddress(recipient));
          message.From = new MailAddress(fromaddress, "Basadur Profile Website");
          message.Subject = subject;
          message.Body = body;
  
          Send(message);
      }
  
      private static void Send(MailMessage msg)
      {
          SmtpClient smtp = new SmtpClient();
          smtp.Host = "smtp.gmail.com";
          smtp.Credentials = new System.Net.NetworkCredential(fromaddress, mailpassword);
          smtp.EnableSsl = true;
          Debug.WriteLine("mailSend: setting up incorrect port....");
          smtp.Port = 5872; //incorrect port
          smtp.SendCompleted += new SendCompletedEventHandler(smtp_SendCompleted);
  
          smtp.SendAsync(msg, msg);
  
      }
  
      static void smtp_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
      {
          var msg = (MailMessage)e.UserState;
  
          if (e.Error != null)
          {
              System.Threading.Thread.Sleep(1000 * 5);
  
              try
              {
                  SmtpClient smtp = new SmtpClient();
                  smtp.Host = "smtp.gmail.com";
                  smtp.Credentials = new System.Net.NetworkCredential(fromaddress, mailpassword);
                  smtp.EnableSsl = true;
                  smtp.Port = 587;
                  smtp.Send(msg); 
              }
              catch (Exception ex)
              {
                  Debug.WriteLine("mailComp: Failed for the second time giving up.");
              }
          }
  
      }
  
  </pre>