Hi
I am getting error - System.Net.Mail.SmtpException
HResult=0x80131500
Message=The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. For more information, go to
Source=System.Net.Mail
StackTrace:
at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)
at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)
at System.Net.Mail.SmtpClient.Send(MailMessage message)
at Atulaya.Form1..ctor() in C:\Users\Administrator\source\repos\Atulaya\Atulaya\Form1.cs:line 97
at Atulaya.Program.Main() in C:\Users\Administrator\source\repos\Atulaya\Atulaya\Program.cs:line 14
I have below code
var addresses = "[email protected];[email protected]";
var fromAddress = new MailAddress("[email protected]", "Sender");
const string fromPassword = "Password";
string subject = "[Message] Message Subject";
string body = "Hi, Test Email";
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
foreach (var address in addresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries))
{
using (var message = new MailMessage(fromAddress.ToString(), address)
{
Subject = subject,
Body = body,
Priority = MailPriority.High
})
{
smtp.Send(message);
}
}
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 7, 2025, 12:18 AM EST
Hello Ramco,
The error you're encountering,
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated, typically indicates that the SMTP server (in this case, Gmail's SMTP server) is rejecting the connection because either:The credentials (username and password) are incorrect.
The connection is not secure (SSL/TLS is not properly configured).
The account has not been configured to allow less secure apps or has 2-factor authentication enabled without an app-specific password.
Here’s a slightly updated version of your code with some improvements:
var addresses = "[email protected];[email protected]"; var fromAddress = new MailAddress("[email protected]", "Sender"); const string fromPassword = "Password"; // Use app-specific password if 2FA is enabled string subject = "[Message] Message Subject"; string body = "Hi, Test Email"; var smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, DeliveryMethod = SmtpDeliveryMethod.Network, UseDefaultCredentials = false, Credentials = new NetworkCredential(fromAddress.Address, fromPassword) }; foreach (var address in addresses.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries)) { using (var message = new MailMessage(fromAddress, new MailAddress(address)) { Subject = subject, Body = body, Priority = MailPriority.High }) { try { smtp.Send(message); Console.WriteLine($"Email sent to {address}"); } catch (Exception ex) { Console.WriteLine($"Failed to send email to {address}: {ex.Message}"); } } }Good Luck!