0

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

  • 2
    Sounds like you need to check if `_smtpSettings.SMTPSendFrom` is set i.e. not empty/null. I would verify that all properties on `_smtpSettings` match the PowerShell version. – phuzi Jun 27 '23 at 15:00
  • Does this answer your question? [5.7.57 SMTP - Client was not authenticated to send anonymous mail during MAIL FROM error](https://stackoverflow.com/questions/30342884/5-7-57-smtp-client-was-not-authenticated-to-send-anonymous-mail-during-mail-fr) – Panagiotis Kanavos Jun 27 '23 at 15:11
  • @phuzi I have tried the username in the send from, I have tried the username with the domain in the from as well. – Steve Schlichter Jun 27 '23 at 15:13
  • @PanagiotisKanavos not so far. I do have an issue where the smtp server in the powershell script is x-y.com and the domain in the from address is x.y.com and the domain preceding the username is x.local It works in the powershell script – Steve Schlichter Jun 27 '23 at 16:07

1 Answers1

0

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

dunxz
  • 288
  • 1
  • 11
  • Though expressed differently, I think this question is a duplicate of https://stackoverflow.com/questions/68484805/how-send-a-single-email-from-an-azure-app-service-webapp – dunxz Jun 27 '23 at 17:14
  • Actually, more suggestions here: https://stackoverflow.com/questions/17666161/sending-email-from-azure – dunxz Jun 27 '23 at 17:16
  • The powershell script is running on the same machine and it is able to send mail. – Steve Schlichter Jun 27 '23 at 17:37