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

354 Views Asked by At

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.

enter image description here

1

There are 1 best solutions below

3
dunxz On

You cannot send email from a hosted Azure AppService (is this what you meant when you descibe "Azure hosted web server."?) This is blocked by policy and cannot be changed. You need to use an API to a third-party (ie SendGrid or other). I'll try and dig out the relevant MS article