Click here to Skip to main content
16,022,296 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am getting exception while trying to send mail "The SMTP server requires a secure connection The client was not authenticated. The server response was: 5.7.57 Client not authenticated" to send mail in c#. Application is running in .Net 4.8 framework. Mail sending done with SSL Authentication and SSL enabled. Code for sending mail is updated below

What I have tried:

SmtpClient smtpClient = new SmtpClient();
                           MailMessage message = new MailMessage();




                           MailAddress fromAddress = new MailAddress(fromMail, "");

                           // You can specify the host name or ipaddress of your server
                           // Default in IIS will be localhost
                           smtpClient.Host = ConfigurationManager.AppSettings["SMTPServer"];
                           smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                           string SMTPAuthenticationFlag = ConfigurationManager.AppSettings["SMTPAuthenticationFlag"] != null ? Convert.ToString(ConfigurationManager.AppSettings["SMTPAuthenticationFlag"].ToString()) : "0";
                           smtpClient.UseDefaultCredentials = false;
                               smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["SMTPUserName"], ConfigurationManager.AppSettings["SMTPPassword"]);


                           int SMTPServerPort = 587;
                           smtpClient.Port = SMTPServerPort;
                           //Added by Alex on 20240625 for sending mail with TLS (Mantis Id-1859) Start
                           string SMTPServerSSLFlag = ConfigurationManager.AppSettings["SMTPServerSSLFlag"] != null ? Convert.ToString(ConfigurationManager.AppSettings["SMTPServerSSLFlag"].ToString()) : "0";
                           if (SMTPServerSSLFlag == "1")
                           {
                               smtpClient.EnableSsl = true;

                           }
                           //Added by Alex on 20240625 for sending mail with TLS (Mantis Id-1859) End
                           //From address will be given as a MailAddress Object
                           message.From = fromAddress;

                           // To address collection of MailAddress
                           message.To.Add(toMail);
                           message.Subject = subject;

                           message.IsBodyHtml = true;

                           // Message body content
                           message.Body = mailContent;

                           // Send SMTP mail
                           smtpClient.Send(message);
Posted
Comments
Richard Deeming 3-Jul-24 10:11am    
REPOST
This is now your THIRD copy of the same question.
How to resolve the exception "authentication failed because the remote party has closed the transport stream" on sending mail in C#?[^]
How to resolve exception "a call to SSPI failed, the function requested is not supported" during mail sending in C#? "[^]

STOP reposting the same question over and over again, and start engaging with the people who have already tried to help you.

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900