.NET 6 SMTP 5.7.57 Client was not authenticated to send anonymous mail from

Steve Schlichter 0 Reputation points
2023-06-27T16:48:37.8033333+00:00
I am trying to send mail via System.Net.Mail

                var to = new MailAddress(notification.SendTo);

                var from = new MailAddress(_smtpSettings.SMTPSendFrom);
                var message = new MailMessage(from, to)
                {
                    Subject = notification.Subject
                    ,
                    Body = notification.Message
                    ,
                    IsBodyHtml = true
                };
                var client = new SmtpClient()
                {
                    Host = _smtpSettings.SMTPServer,
                    Port = _smtpSettings.SMTPPort,
                    EnableSsl = _smtpSettings.SMTPUseSSL,

                    UseDefaultCredentials = false,
                    DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network
                };
                if (_smtpSettings.SMTPDomain == string.Empty)
                {
                    client.Credentials = new NetworkCredential(_smtpSettings.SMTPLogin, _smtpSettings.SMTPPassword);
                }
                else
                {
                    client.Credentials = new NetworkCredential(_smtpSettings.SMTPLogin, _smtpSettings.SMTPPassword, _smtpSettings.SMTPDomain);
                }

                client.Send(message);

The error I get is 5.7.57 Client was not authenticated to send anonymous MAIL FROM. I have parameterized every setting that I can think of. Tried sending the domain, UseDefaultCredentials true / false.

I also have powershell script that I use and CAN send mail via the script using the same account from the same machine (logged in as me while the app is logged in via a App Pool ID).

Some added info: The mail server is an Exchange server. The machine I am sending from is an Azure hosted web server.

Kind of stumped.

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | C#
{count} votes

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.