title: Getting error while sending bulk email "An asynchronous call is already in progress. It must be completed or canceled before you can call this method"
body: <p>I have created one console application for migrating data from one database table to another in which records of customers are migrated so I have to notify them for password change. </p>
  
  <pre><code>public static async Task&lt;bool&gt; SendRegisterEmail(List&lt;MailMessage&gt; mailMessage)
  {
      bool flag = true;
      try
      {
          var smtp = new SmtpClient();
          var taskEmails = mailMessage.Select(x =&gt; smtp.SendMailAsync(x));
  
          await Task.WhenAll(taskEmails); // **Error : An asynchronous call is already in progress. It must be completed or canceled before you can call this method**
      }
      catch (Exception ex)
      {
          throw ex;
      }
  
      return flag;
  }
  </code></pre>
  
  <p>emails are successfully sent asyncronously if I remove that <strong>await Task.WhenAll(taskEmails)</strong> but when migration operation completes,it not sends all the emails. many of them remain unsend on console app closed after operation completed and I have more than 1,00,000 records so how can I continue email sending process in background or application running until the all emails are successfully sent ?</p>
  
  <p>Here is the code of data migration:</p>
  
  <pre><code>foreach (DataRow SourceReader in DS.Tables[0].Rows)
  {
      insertCounter++;
      using (SqlCommand DestinationCommand = DestinationConnection.CreateCommand())
      {
          Console.WriteLine("inserting row...");
          var date = (SourceReader["LockedUntil"] == DBNull.Value ? Convert.ToDateTime("01/01/1753") : Convert.ToDateTime(SourceReader["LockedUntil"]));
          DestinationCommand.CommandText = string.Format(insertQuery1, (SourceReader["CustomerGUID"] == DBNull.Value ? null : SourceReader["CustomerGUID"].ToString()), (SourceReader["FirstName"] == DBNull.Value ? null : SourceReader["FirstName"].ToString()), (SourceReader["LastName"] == DBNull.Value ? null : SourceReader["LastName"].ToString()), (SourceReader["Email"] == DBNull.Value ? null : SourceReader["Email"].ToString()), 1, PasswordManager.Encrypt(SourceReader["FirstName"].ToString() + "1!"), (SourceReader["Phone"] == DBNull.Value ? null : SourceReader["Phone"].ToString()), 1, 0, 0, date.ToString("yyyy-MM-ddTHH:mm:ss"), Convert.ToInt16(SourceReader["BadLoginCount"] == DBNull.Value ? 0 : SourceReader["BadLoginCount"]), Convert.ToInt16(SourceReader["OkToEmail"] == DBNull.Value ? 0 : SourceReader["OkToEmail"]), (SourceReader["CustomerGUID"] == DBNull.Value ? null : SourceReader["CustomerGUID"].ToString()), 2);
          DestinationCommand.ExecuteNonQuery();
          Console.WriteLine("AspNetUser Row inserted...!!! ");
      }
      if ((insertCounter % 100) == emailCounter)
      {
          var message = Email.AddUserForEmail(new User() { Email = (SourceReader["Email"] == DBNull.Value ? null : SourceReader["Email"].ToString()), Password = (SourceReader["FirstName"].ToString() + "1!"), FirstName = (SourceReader["FirstName"] == DBNull.Value ? null : SourceReader["FirstName"].ToString()), LastName = (SourceReader["LastName"] == DBNull.Value ? null : SourceReader["LastName"].ToString()) });
          mailList.Add(message);
          var flag = Email.SendRegisterEmail(mailList);
          emailCounter++;
      }
      else if (insertCounter == totalCount)
      {
          var message = Email.AddUserForEmail(new User() { Email = (SourceReader["Email"] == DBNull.Value ? null : SourceReader["Email"].ToString()), Password = (SourceReader["FirstName"].ToString() + "1!"), FirstName = (SourceReader["FirstName"] == DBNull.Value ? null : SourceReader["FirstName"].ToString()), LastName = (SourceReader["LastName"] == DBNull.Value ? null : SourceReader["LastName"].ToString()) });
          mailList.Add(message);
          var flag = Email.SendRegisterEmail(mailList);
      }
      else
      {
          var message = Email.AddUserForEmail(new User() { Email = (SourceReader["Email"] == DBNull.Value ? null : SourceReader["Email"].ToString()), Password = (SourceReader["FirstName"].ToString() + "1!"), FirstName = (SourceReader["FirstName"] == DBNull.Value ? null : SourceReader["FirstName"].ToString()), LastName = (SourceReader["LastName"] == DBNull.Value ? null : SourceReader["LastName"].ToString()) });
          mailList.Add(message);
      }
      // var i = Email.SendRegisterEmail((SourceReader["Email"] == DBNull.Value ? null : SourceReader["Email"].ToString()), (SourceReader["FirstName"].ToString() + "1!"), (SourceReader["FirstName"] == DBNull.Value ? null : SourceReader["FirstName"].ToString()), (SourceReader["LastName"] == DBNull.Value ? null : SourceReader["LastName"].ToString()));
  }
  </code></pre>