SMTP client has been used to send mail to the SMTP server. While trying to send mail using Gmail account as network credential, I faced an issue, “SMTP server requires a secure connection“. This is a recent issue with SMTP connection as below. This is because from May 30, 2022, Google has stopped supporting this feature. Let’s see the below steps to rectify the mentioned issue in this blog.
Instead, we have to enable 2 factor authentication and create “App passwords” in respective Gmail account (From which the mail has to be sent).
Please find the below code snippet for sending mail with “Gmail” host.
MailMessage message = new MailMessage();
message.From = new MailAddress("<from mail>","<Display name>");
message.To.Add("<To mail>");
message.Subject = "Test";
message.Body = "Welcome";
message.IsBodyHtml = true;
SmtpClient SmtpClient = new SmtpClient();
SmtpClient.Credentials = new System.Net.NetworkCredential("<from mail>","<Password>");
SmtpClient.Host = "smtp.gmail.com";
SmtpClient.Port = 587;
SmtpClient.EnableSsl = true;
SmtpClient.Send(message);
The above .NET code has been used to send mail from “Gmail” host .In this <password> – is the Gmail account password. When this solution is executed, user will be facing the below issue.

This is because from May 30 2022, Google has stopped supporting this feature. Instead we have to enable 2 factor authentication and create “App passwords” in respective gmail account (From which the mail has to be sent).
To create App password :
Login to the respective Gmail Account->Click Profile icon ->Choose Manage your Google account ->Security ->How you sign in to Google->Enable 2 Step verification.

Once enabled, look for “App passwords” and create new App password and replace the <Password> tag in code snippet with the created App password.

Now, we can use this app password in the above code snippet instead of <Password> and it will work.
Similar to Gmail, we can use office account for sending mail.
Replace SMTP host as below
SmtpClient.Host = “smtp.office365.com”;
For office account also App password should be created and used in place of <Password>.
Here security has been enhanced using App password instead of password and 2 factor authentication.
Hope this blog helps in solving the above-mentioned SMTP issue.
No Comment! Be the first one.