title: Sending Email asynchronously in ASP.NET C#
body: <p>I was wondering if theres a way to send email asynchronously in asp.net c#. I've used the following code to send the emails:</p>
  
  <pre><code> if (checkObavijest.Checked)
              {
                  List&lt;hsp_Kupci_Newsletter_Result&gt; lista = ServisnaKlasa.KupciNewsletter();
                  for (int i = 0; i &lt; lista.Count; i++)
                  {
                      MailMessage mail = new MailMessage();
  
                      mail.From = new MailAddress("*******");
                      mail.To.Add(lista[i].Email);
                      mail.Subject = "Subject";
                      mail.SubjectEncoding = Encoding.UTF8;
                      mail.BodyEncoding = Encoding.UTF8;
                      mail.IsBodyHtml = true;
                      mail.Priority = MailPriority.High;
                      mail.Body = "Some message";
                      SmtpClient smtpClient = new SmtpClient();
                      Object state = mail;
                      smtpClient.Credentials = new NetworkCredential("****@gmail.com", "****");
                      smtpClient.Port = 587;
                      smtpClient.Host = "smtp.gmail.com";
                      smtpClient.EnableSsl = true;
                      smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
                      try
                      {
                          smtpClient.SendAsync(mail, state);
                      }
                      catch (Exception ex)
                      {
  
                      }
                  }
  
   void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
          {
  
              MailMessage mail = e.UserState as MailMessage;
  
              if (!e.Cancelled &amp;&amp; e.Error != null)
              {
                  string poruka = "Emailovi poslani!";
                  ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + poruka + "');", true);
              }
          }
  </code></pre>
  
  <p>But when I tick the option for sending emails, the page loads like 40 seconds, then after 40 seconds emails are sent then. As you can see I'm pulling out like 20-30 emails currently out of my DB. I thought this was the correct way to send mails asynchronously, but apparently its not... How can I push the email sending to another thread so it doesn't affects the user who is using this page? Can someone help me out with this ? Thanks !</p>