title: Send mail with smtp SendAsync
body: <p>this is how when I need to send email gives me error. But the mistake that since gives me is this:</p>
  
  <blockquote>
    <p>An asynchronous operation cannot be started at this time. Asynchronous
    operations may only be started within an asynchronous handler or
    module or during certain events in the Page lifecycle. If this
    exception occurred while executing a Page, ensure that the Page is
    marked &lt;%@ Page Async="true" %>. This exception may also indicate an
    attempt to call an "async void" method, which is generally unsupported
    within ASP.NET request processing. Instead, the asynchronous method
    should return a Task, and the caller should await it.</p>
  </blockquote>
  
  <p>I have accumulated since the MVC and have used class to keep track of ie areas of the page. The reason I have used SendAsync is precisely that it goes a little faster to send email, etc..</p>
  
  <p>This error only happens when I try to send email to users.</p>
  
  <pre><code>public static void NewPassword(string mail, string name, string password)
      {
          MailDefinition oMailDefinition = new MailDefinition();
          oMailDefinition.BodyFileName = "~/MailList/emailskabelon/NewPassword.html";
          oMailDefinition.From = FromMail;
  
          Dictionary&lt;string, string&gt; oReplacements = new Dictionary&lt;string, string&gt;();
          oReplacements.Add("&lt;&lt;navn&gt;&gt;", name);
          oReplacements.Add("&lt;&lt;password&gt;&gt;", password);
  
          System.Net.Mail.MailMessage oMailMessage = oMailDefinition.CreateMailMessage(mail, oReplacements, new LiteralControl());
          oMailMessage.Subject = NewpasswordTitle + WebsiteName;
          oMailMessage.IsBodyHtml = true;
  
          SmtpClient smtp = new SmtpClient(AzureApi);
          System.Net.NetworkCredential netcred = new System.Net.NetworkCredential(AzureName, AzurePassword);
          smtp.UseDefaultCredentials = false;
          smtp.EnableSsl = true;
  
          smtp.Credentials = netcred;
          smtp.Port = Convert.ToInt32("25");
          smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
  
          using (var smtpClient = new SmtpClient())
          {
              smtp.SendAsync(oMailMessage, null);
          }
      }
  </code></pre>
  
  <p>I've tried to do like this:</p>
  
  <pre><code>public static async NewPassword(string mail, string name, string password)
          {
              ....
              using (var smtpClient = new SmtpClient())
              {
                  await smtp.SendAsync(oMailMessage, null);
              }
  </code></pre>
  
  <p>i have see here: <a href="https://stackoverflow.com/a/35212320/7391454">https://stackoverflow.com/a/35212320/7391454</a></p>