SMTP send throws SmtpException only from within Docker container

I’m trying to send a mail within a Docker container, this works fine locally when I run code on IIS from Visual Studio. On the server in a Docker container on the other hand it throws an exception:

SmtpException: The SMTP server requires a secure connection or the
client was not authenticated. The server response was: 5.7.0
Authentication Required.

Things I already tried:

  • I already enabled secure apps on my Google account
  • UseDefaultCredentials true and false
  • EnableSsl true and false
  • Changed order of UseDefaultCredentials, Credentials and EnableSsl
  • Exposed port 587/tcp on docker container


                    MailMessage message = new MailMessage();
                    SmtpClient smtp = new SmtpClient();
                    message.From = new MailAddress("[email protected]");
                    message.To.Add(new MailAddress(Environment.GetEnvironmentVariable("MAIL_TO")));
                    message.Subject = "subject";
                    message.IsBodyHtml = true;
                    message.Body = GetHtmlTable(workedHours, currentWeeknumber);
                    smtp.Port = 587;
                    smtp.Host = "";
                    smtp.UseDefaultCredentials = false;
                    smtp.Credentials = new NetworkCredential("[email protected]", "password");
                    smtp.EnableSsl = true;
                    smtp.DeliveryMethod = SmtpDeliveryMethod.Network;


What am I doing wrong? All the posts solve their issue by enable less secure apps.

Source: StackOverflow