title: Email sending working in localhost but when not in published web on server in ASP.NET MVC I am using 587 port
body: <p>I am trying to send mail from Gmail and it's sent successfully from localhost, but when I publish the website to the server, it didn't send it from there.</p>
  <p>Any help?</p>
  <p>This is my code - ASP.NET Core 5.0 using for the API project and this is the code how can I resolve this issue please help me as soon as possible thanks in advance</p>
  <pre><code>[HttpPost , ValidateAntiForgeryToken]
  [Route(&quot;Email/Post&quot;)]
  public bool? Post([FromBody] ModEmail l_ModEmail)
  {
      try
      {
          string l_Mail_From = &quot;email@gmail.com&quot;;
          string l_Email_Password = &quot;password&quot;;
  
          // Create Mail Message Object
          MailMessage l_MailMessage = new MailMessage(l_Mail_From,l_ModEmail.Pr_EmailTo, 
          l_ModEmail.Pr_Subject, l_ModEmail.Pr_Body);
  
          l_MailMessage.IsBodyHtml = true;
  
          // Create SmtpClient class to Send Message             
          SmtpClient l_SmtpClient = new SmtpClient();
  
          // Here you specify your smtp host address such as smtp.myserver.com
          l_SmtpClient.Host = &quot;smtp.gmail.com&quot;;
          l_SmtpClient.Port = 587;
          l_SmtpClient.EnableSsl = true;
  
          // Specify that you don't want to use default credentials
          l_SmtpClient.UseDefaultCredentials = false;
  
          // Create user credentials by using NetworkCredential class 
          NetworkCredential l_NetworkCredential = new NetworkCredential();
          l_NetworkCredential.UserName = l_Mail_From;
          l_NetworkCredential.Password = l_Email_Password;
          l_SmtpClient.Credentials = l_NetworkCredential;
          l_SmtpClient.Send(l_MailMessage);
  
          return true;
      }
      catch (Exception ex)
      {
          Debug.WriteLine(ex.Message);
          return null;
      }
  }
  </code></pre>