Unable to send email from Azure App service using Mailkit, but printer/scanner can send fine

CL 1 Reputation point
2022-05-23T18:29:18.263+00:00

I get an error:
MailKit.Security.AuthenticationException: 535: 5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully.

We're using port 587 with TLS 1.2. The IP Address of the App Service is whitelisted.

            using (var client = new SmtpClient())
            {
                client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
                await client.ConnectAsync(_emailSettings.MailServer, _emailSettings.MailPort, SecureSocketOptions.StartTls);

                System.Security.SecureString psw = new System.Security.SecureString();
                foreach (char item in _emailSettings.Password.ToCharArray())
                {
                    psw.AppendChar(item);
                }
                await client.AuthenticateAsync(new NetworkCredential(_emailSettings.Sender, psw));
                await client.SendAsync(message);
                await client.DisconnectAsync(true);
            }

Any help would be greatly appreciated. thanks

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,299 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ryan Hill 29,666 Reputation points Microsoft Employee
    2022-05-24T17:17:28.15+00:00
    0 comments No comments

  2. Nick Beckley 1 Reputation point
    2022-06-22T15:02:49.267+00:00

    I've had similar today and it turns out that basic authentication has been turned off with Microsoft 365 email. You now have to use OAUTH2. Of course it might be something very different but I know of others this has happened to.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.