I'm getting "Server does not support secure connections error" while trying to send mail through google smtp.
below is my code:
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
string to = "recepient@gmail.com"; //To address
string from = "sender@gmail.com"; //From address
MailMessage message = new MailMessage(from, to);
string mailbody = "test gmail test boiiixi";
message.Subject = "boi gmail mail";
message.Body = mailbody;
message.BodyEncoding = Encoding.UTF8;
message.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587); //Gmail smtp
client.UseDefaultCredentials = false;
System.Net.NetworkCredential basicCredential1 = new System.Net.NetworkCredential("sender@gmail.com", "myAppPassword");
client.EnableSsl = true;
client.Credentials = basicCredential1;
try
{
client.Send(message);
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "sent successfully";
}
catch (Exception ex)
{
Label1.ForeColor = System.Drawing.Color.Red;
Label1.Text = "error = " + ex.Message;
}
I tried setting
client.EnableSsl = false
;A lot posts I have seen regarding this issue said to enable Less Secure Apps option. But Google has now removed the option.
But when I tested my credentials and tried to send mail via some smtp tester site for example www.smtper.net. It works and the mail is getting sent. What am I doing wrong?
I'm using .net 4.0
UPDATE: I have noticed something weird. when I ran the same code in my production server using my 'PERSONAL' gmail account credentials, it works !!!! but when tried using WORK account credentials it fails with error "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.0 Authentication Required. Learn more at" even though my 2 step verification is ON .