suraj
suraj

Reputation: 51

How to resolve a google mail error "The SMTP server requires secure connection or the client was not authenticated"?

MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress("[email protected]");
mailMessage.To.Add("[email protected]");
mailMessage.Subject = "New Enquiry" ;

mailMessage.Body = "<b>Sender Name : </b>" + txt_name.Text + "<br/>"
    + "<b>Contact Number : </b>" + txt_number.Text + "<br/>"
    + "<b>Sender Email : </b>" + txt_email.Text + "<br/>"
    + "<b>Details : </b>" + txt_message.Text;
mailMessage.IsBodyHtml = true;


SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = 
    new System.Net.NetworkCredential("[email protected]", "Password");
smtpClient.Send(mailMessage);

Response.Write("<Script>alert('Thanks for contact us,our team will be contact you as soon as possible')</Script>");

txt_name.Text = "";
txt_email.Text = "";
txt_number.Text = "";
txt_message.Text = "";

This is my ASP.NET C# code for sending emails through the website's contact us page. But Google has discontinued the less secure app access option as of May 30, 2022 due to security reasons. So now I'm not able to send emails using the above code.

Less Secure app access snapshot:

Less Secure app access snapshot

How can I deal with this so I can send email again?

Upvotes: 5

Views: 8480

Answers (2)

user29206711
user29206711

Reputation: 1

Try using below code along with above answer:

var smtpClient = new SmtpClient("smtp.gmail.com") { 
    Port = 587,
    Credentials = new NetworkCredential("gmail emailid", "App Password"), 
    EnableSsl = true,
};

Upvotes: 0

NajiMakhoul
NajiMakhoul

Reputation: 1717

https://support.google.com/accounts/answer/6010255

from May 30, 2022, ​​Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password.

Important: This deadline does not apply to Google Workspace or Google Cloud Identity customers. The enforcement date for these customers will be announced on the Workspace blog at a later date.

Now you can manage Apps password:

  1. Go to Gmail account settings , and click on security tab
  2. add 2 step verification (if you don't have)
  3. click on "App Password"
  4. select application device or add new one
  5. you will get new password to use in your app

change the email password in your app , and try to send emails

Upvotes: 17

Related Questions