title: Is this REALLY async email AND best practice?
body: <p>I am trying to learn and master ASYNC and AWAIT but it is not clear if what I do is the right thing at then end.. I mean the code works and I took two browsers/instances of the same form and click, well, almost at the same time and everything went OK. I received 2 emails but is it the right way to do it? and does it really gives a plus value to do it async instead of sync...</p>
  
  <p>This is my code:</p>
  
  <pre><code>    [HttpPost]
      [AllowAnonymous]
      [ValidateAntiForgeryToken]
      public async Task&lt;ActionResult&gt; Register(RegisterViewModel model)
      {
         bla bla do some sync simple stuffs etc...
         ...
         await Task.Run(() =&gt; Utility.MailUtility.SendEmail(user.Email, user.To, msg, subj));
         return RedirectToAction("Index", "Home");
      }
  </code></pre>
  
  <p>and then the mailing code looks like this, as per a sample code taken from stackoverflow:</p>
  
  <pre><code>    public static async Task SendEmail(params etc...) 
      {
          await SendMailForTemplateID(params etc...);
      }
  
      private static Task SendMailForTemplateID(params etc...)
      {
  
  
              var task = Task.Factory.StartNew(() =&gt;
              {
                  MailMessage message = new MailMessage();
                  MailMessage mail = new MailMessage();
                  //SmtpClient SmtpServer = new SmtpClient(ConfigurationManager.AppSettings["SMTPHost"]);
                  SmtpClient SmtpServer = new SmtpClient("localhost"); // for test purpose only
                  mail.From = new MailAddress(from);
                  mail.To.Add(to);
                  mail.Subject = GetSubjectForTemplateId(templateID, extraMsg, dataInfo, fromName, toName, isFr);
                  mail.Body = GetBodyForTemplateId(templateID, extraMsg, dataInfo, fromName, toName, isFr);
                  mail.IsBodyHtml = false; 
                  SmtpServer.Port = 81; // for test purpose only
                  SmtpServer.Credentials = new System.Net.NetworkCredential
                      (ConfigurationManager.AppSettings["CredentialUser"],
                      ConfigurationManager.AppSettings["CredentialPassword"]);
  
                  bool result;
                  bool.TryParse(ConfigurationManager.AppSettings["EnableSSL"], out result);
                  SmtpServer.EnableSsl = result;
  
                  SmtpServer.Send(mail);
  
                  //* make sure we leave nothing in memory
                  message.Dispose();
                  mail.Dispose();
                  SmtpServer.Dispose();
  
  
              });
  
              return task;
  
         }
  </code></pre>
  
  <hr>
  
  <p>I am sorry guys but I don't know how to put line return (enter) between my replies. Is It possible? nway, regarding ASYNC-EMAIL best practice.</p>
  
  <p>My new version of code looks like this, it works more simple coding but is it right? I mean no new threads, does it give a plus value VS sync sendmail methods etc...</p>
  
  <p>I call it in a MVC action. The code:</p>
  
  <pre><code>     do stuff...
       await Utility.MailUtility.SendMailForTemplateID(params);
       do stuff...
  
     public static async Task SendMailForTemplateID(params...)
      {
          try
          {
              email code etc...
  
              await smtpClient.SendMailAsync(mail);
  
              mail.Dispose();
              smtpClient.Dispose();
  
  
          }
          catch (Exception)
          {
              // Log ex
          }
      }
  </code></pre>