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

Amit MohantyRank #112 · 20K reputationPosted Nov 10, 2023, 1:38 AM EST
Ensure that you're providing the correct credentials for the SMTP server. You might need to set the
NetworkCredentialproperty for the SMTP client.Lokesh VarmanRank #374 · 6.8K reputationPosted Nov 11, 2023, 3:30 AM EST
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:
using (SmtpClient smtp = new SmtpClient()) { // Assuming these values are set in your appsettings.json string smtpHost = _configuration.GetValue(Constants.EmailServer + Constants.SmtpHost); int smtpPort = _configuration.GetValue(Constants.EmailServer + Constants.Port); string smtpUsername = _configuration.GetValue(Constants.EmailServer + Constants.Username); string smtpPassword = _configuration.GetValue(Constants.EmailServer + Constants.Password); smtp.Port = smtpPort; smtp.Host = smtpHost; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; // Set the credentials smtp.Credentials = new NetworkCredential(smtpUsername, smtpPassword); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; try { await smtp.SendMailAsync(message); Console.WriteLine("Email sent successfully"); } catch (SmtpException ex) { Console.WriteLine($"Error sending email: {ex.Message}"); } }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.Tuhin PaulRank #7 · 71.9K reputationPosted Nov 11, 2023, 2:15 PM EST
using (SmtpClient smtp = new SmtpClient()) { smtp.Port = _configuration.GetValue(Constants.EmailServer + Constants.Port); smtp.Host = _configuration.GetValue(Constants.EmailServer + Constants.SmtpHost); smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; // Provide your SMTP server username and password smtp.Credentials = new NetworkCredential("YourSmtpUsername", "YourSmtpPassword"); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; await smtp.SendMailAsync(message); }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.
Anandu G NathRank #681 · 3.5K reputationPosted Jan 25, 2024, 10:56 PM EST
using (SmtpClient smtp = new SmtpClient()) { smtp.Port = configuration.GetValue(Constants.EmailServer + Constants.Port); smtp.Host = configuration.GetValue(Constants.EmailServer + Constants.SmtpHost); smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Credentials = new NetworkCredential("YourUsername", "YourPassword"); smtp.DeliveryMethod = SmtpDeliveryMethod.Network; await smtp.SendMailAsync(message); }Tuhin PaulRank #7 · 71.9K reputationPosted Nov 11, 2023, 2:23 PM EST
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.