title: Stop form from closing before getting response from SmtpClient.SendCompleted Event
body: <p>I am using SmtpClient to sent out email. I create some function in <code>Mail class</code>:</p>
  
  <pre><code>private void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
  {
      // Get the unique identifier for this asynchronous operation.
      String token = (string)e.UserState;
  
      if (e.Cancelled)
      {
          MailStatus = Status.CANCEL;
          MailStatusMessage = token + " Send canceled.";
      }
      else if (e.Error != null)
      {
          MailStatus = Status.ERROR;
          MailStatusMessage = token + " " + e.Error.ToString();
      }
      else
      {
          MailStatus = Status.SENT;
          MailStatusMessage = "Mail sent.";
      }
  
      mailSent = true;
  }
  
  public void SentEmail()
  {
      client = new SmtpClient(Host, Port);
      client.Credentials = new NetworkCredential(UserName, Password);
      MailAddress from = new MailAddress(MerchantEmail, MerchantName);
      MailAddress to = new MailAddress(CustomerEmail);
      MailMessage message = new MailMessage(from, to);
      message.Body = EmailSubjectTemplate();
      message.BodyEncoding = System.Text.Encoding.UTF8;
      message.Subject = EmailSubjectTemplate();
      message.SubjectEncoding = System.Text.Encoding.UTF8;
  
      client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);
  
      client.SendAsync(message, "Sending message.");
  
      message.Dispose();
  }
  </code></pre>
  
  <p>In a form I call the function, before closing the form, but when waiting for the response from the SendCompletedCallback, the this.Close() will be executed:</p>
  
  <pre><code>Mail mail = new Mail();
  mail.SentEmail();
  this.Close();
  </code></pre>
  
  <p>How can I stop the form from closing before I get a response from SendCompletedCallback?</p>