2

I am trying to send an email from asp.net core application but I keep getting an error that reads

SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. [JN2P275CA0038.ZAFP275.PROD.OUTLOOK.COM]

I have also tried checking firewall rules and increasing timeout and that has not helped so I am not sure what is wrong

This is my code to send email

   SmtpClient client = new SmtpClient("smtp.office365.com")
                    {
                        //Port = 587, //outlook port 587
                        Port = 587, 
                        EnableSsl = true,
                        //UseDefaultCredentials
                        UseDefaultCredentials = false,
                        TargetName = "STARTTLS/smtp.office365.com",
                        DeliveryMethod = SmtpDeliveryMethod.Network,
                        Credentials = new NetworkCredential("myemail@myemails.com","password"),
                    };

                    MailMessage message = new MailMessage()
                    {
                        From = new MailAddress("myemail@myemails.com"),
                        Subject = "New Requisition",
                        Body = "New requisition has been created:" + " " + requisition.RequisitionNo,
                        IsBodyHtml = true,
                    };

                    message.To.Add(SubmissionNotfication.To);
                    //message.CC.Add(SubmissionNotfication.Cc);
                    //var attachment = new System.Net.Mail.Attachment(filePath); //Attachment(string fileName, string mediaType) 
                    //message.Attachments.Add(attachment);
                    client.Send(message);
                }
        
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zidane
  • 1,696
  • 3
  • 21
  • 35
  • There could be two reasons for error. If you are inside a corporate network with Outlook Mail Server you cannot access an different Mail Server. All 587 ports get forwarded to the Outlook Proxy Server for authentication. Try code from home to see if it works. If you are inside a corporate network you may not need a secure connection and using port 25 may work. When you are inside a firewall the extra protection is not needed and encrypting email will make it difficult for virus checkers to verify the mail. – jdweng Jun 16 '21 at 06:35
  • Is there a chance that the used password contains some "foreign" characters like a german umlaut? – Heslacher Jun 16 '21 at 06:35
  • hmm.. it's hard to say without assurances with your config/settings and client being correct. In fact, it's unlikely to be correct in that respect. apart from that, the "Client not authenticated to send mail." seems like an odd message. In regards to my understanding on authentication and authorization, I would think it ought to be written "Client not [authorized] to send mail." have you verified whether the client has the authorization to do that in respect to an API invoking it? – Brett Caswell Jun 16 '21 at 07:05
  • also, did you try specifying the IP address as host (I'm unsure of the `TargetName` portion.. perhaps it isn't registered by name and isn't resolvable by name?) – Brett Caswell Jun 16 '21 at 07:07
  • this question is essentially a duplicate of this one https://stackoverflow.com/questions/6244694/send-smtp-email-using-system-net-mail-via-exchange-online-office-365 . Though that one doesn't have an accepted answer and was posed against beta. It has several answers that are more modern and relate to this issue. – Brett Caswell Jun 16 '21 at 07:26
  • or perhaps not exactly equivalent, as that relates to `anonymous mail`.. invert indication that this is probably related to permissions of the user you're using – Brett Caswell Jun 16 '21 at 07:31

2 Answers2

1

Have a look at How to set up an application to send email using Microsoft 365 or Office 365

You might have to use your MX endpoint as the sending domain and try port 25

SmtpClient client = new SmtpClient("yourorganisation.mail.protection.outlook.com")
{
    Port = 25, 
    EnableSsl = true,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential("myemail@myemails.com","password"),
};

Rosco
  • 2,108
  • 17
  • 17
1

It could be few reasons you encounter above error message

  1. Incorrect email usernmae and password
  2. SSL settings. Make sure you enable SSL security while sending emails through your application
  3. Your port issue. Make sure that your ISP doesn't block port 25 or 587. You can check it by using telnet.

You may need to check tutorial about send email using outlook. If you are using Gmail, then you can refer to this post https://dotnetblog.asphostportal.com/using-gmail-to-send-email-in-asp-net-core/

TheGunners
  • 127
  • 2