Error for mail triggering:System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; the Client was not authenticated to send anonymous mail during MAIL FROM'
Code :
using (SmtpClient smtp = new SmtpClient())
{
smtp.Port = _configuration.GetValue
smtp.Host = _configuration.GetValue
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
await smtp.SendMailAsync(message);
}
values are set in appsettings.json
Anandu G NathPosted Jan 26, 2024, 3:56 AM
Tuhin PaulPosted Nov 11, 2023, 7:23 PM
Dear @Lokesh, you code is straight to the point but I have found some observation such as the code catches SmtpException but doesn't handle it other than printing an error message. You might want to log or handle exceptions differently, depending on your application's requirements. Also the configuration values (SMTP host, port, username, and password) are properly validated before using them. Invalid or missing configuration values can lead to runtime errors.
Tuhin PaulPosted Nov 11, 2023, 7:15 PM
SMTP client (SmtpClient) is configured with the correct username and password for authentication. You should set the NetworkCredential property on the SmtpClient object to provide the necessary credentials.
Lokesh VarmanPosted Nov 11, 2023, 8:30 AM
The error you're encountering suggests that the SMTP server you're trying to connect to requires authentication, and your code is not providing the necessary credentials.
Here's an example of how you can modify your code to include credentials:
Make sure to replace
Constants.UsernameandConstants.Passwordwith the appropriate keys from your appsettings.json file. Also, ensure that the provided credentials are valid for the SMTP server you are trying to connect to.Amit MohantyPosted Nov 10, 2023, 6:38 AM
Ensure that you're providing the correct credentials for the SMTP server. You might need to set the
NetworkCredentialproperty for the SMTP client.