title: Send email async fail in Asp net
body: <p>I send emails via Asp .net, When I tried to send them in async mode, it didnt work, arised an error:<code>
  Failure sending mail. ---&gt; System.InvalidOperationException: Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.</code></p>
  
  <p>Here is my code:</p>
  
  <pre><code>    MailMessage message = new MailMessage();
      message.From = new MailAddress("chani.poz@gmail.com");
      message.To.Add(new MailAddress(to));
      message.Subject = subject;
      message.Body = body;
  
      SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
      SmtpServer.Port = 587;
      SmtpServer.Credentials = new NetworkCredential(userName, pass);
      SmtpServer.EnableSsl = true;
      SmtpServer.Send(message); //sends successful
      SmtpServer.SendCompleted += SmtpServer_SendCompleted;
      SmtpServer.SendAsync(message, null); //failure sending
  </code></pre>