I'm not sure if you have modified the setting in Less secure app access setting to "turn on", so that you can use gmail to send mail in third-party applications.
According to the code you provided, you also need to set UseDefaultCredentials property to "false", something like this:
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
24 Points
90 Posts
Error - 'The SMTP server requires a secure connection or the client was not authenticated
Dec 30, 2020 02:32 AM|jagjit saini|LINK
Hi
<div>protected void sendEmail(string address, string subject, string from, string body)</div> <div> {</div> <div> System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();</div> <div> message.To.Add(address);</div> <div> message.Subject = subject;</div> <div> message.From = new System.Net.Mail.MailAddress(from);</div> <div> message.Body = body;</div> <div> System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");</div> <div> smtp.Credentials = new System.Net.NetworkCredential("abc@gmail.com", "XXXXX");</div> <div> smtp.Port = 587;</div> <div> smtp.EnableSsl = true;</div> <div> smtp.Send(message);</div> <div> </div> <div> }</div>
Thanks
Participant
1780 Points
553 Posts
Re: Error - 'The SMTP server requires a secure connection or the client was not authenticated
Dec 31, 2020 02:41 AM|XuDong Peng|LINK
Hi jagjit saini,
I'm not sure if you have modified the setting in Less secure app access setting to "turn on", so that you can use gmail to send mail in third-party applications.
According to the code you provided, you also need to set UseDefaultCredentials property to "false", something like this:
protected void sendEmail(string address, string subject, string from, string body) { System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add(address); message.Subject = subject; message.From = new System.Net.Mail.MailAddress(from); message.Body = body; System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com"); smtp.Credentials = new System.Net.NetworkCredential("test@gmail.com", "xxxx"); smtp.Port = 587; smtp.EnableSsl = true; smtp.UseDefaultCredentials = false; smtp.Send(message); }
Hope this can help you.
Best regards,
Xudong Peng