0

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;
}
  1. I tried setting client.EnableSsl = false;

  2. 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 .

user8883996
  • 133
  • 2
  • 7
  • `ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;` why do you have this line there? Why do you think you need to manually set the TLS version? Also which version of Windows are you running, and which version of .NET, exact build numbers please? – Charlieface May 02 '23 at 12:27
  • As often with this questions it could be that SmtpClient simply can't do it. From the docs "We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub." – Ralf May 02 '23 at 12:57
  • @Ralf `SmtpClient` works fine for GMail so something else is going on. Possibly the creds are incorrect, or `ServicePointManager.SecurityProtocol` should be removed – Charlieface May 02 '23 at 13:07
  • @Charlieface even after removing the securityProtocol line I'm getting the same error. And yes ! my credentials are absolutely right! as they seem to work on the site that i have just mentioned on my post – user8883996 May 02 '23 at 14:20
  • @Charlieface my exact .NET build => v4.0.30319 running on windows 10 pro 64 bit build 19044. checked the credentials atleast 10 times . commented the ServicePointManager.SecurityProtocol but still getting exact same error – user8883996 May 03 '23 at 06:29
  • That's just a general version number for .NET 4. See https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed for the exact version. Is your Windows fully up to date? – Charlieface May 03 '23 at 09:19
  • @Charlieface yes. my windows version is fully up to date. Target version of web project id is 4.5.2 and the latest version installed is .NET Framework Version: 4.8 – user8883996 May 03 '23 at 10:58
  • Maybe target a higher version. I think above 4.6 will turn on TLS 1.2 https://stackoverflow.com/questions/34920429/is-tls-1-1-and-tls-1-2-enabled-by-default-for-net-4-5-and-net-4-5-1 – Charlieface May 03 '23 at 11:12
  • oh okay. that is why i had used ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; which turns on Tls1.2 – user8883996 May 03 '23 at 11:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/253448/discussion-between-charlieface-and-user8883996). – Charlieface May 03 '23 at 13:22
  • I don't think that's going to work. 4.5 just doesn't support it at all, and it's way out of date now. – Charlieface May 03 '23 at 13:24

1 Answers1

0

This is the code I use.

class Program
    {
        private const string To = "[redacted]@gmail.com";
        private const string From = "[redacted]@gmail.com";
        
        private const string GoogleAppPassword = "";
        
        private const string Subject = "Test email";
        private const string Body = "<h1>Hello</h1>";
        
        
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            
            var smtpClient = new SmtpClient("smtp.gmail.com")
            {
                Port = 587,
                Credentials = new NetworkCredential(From , GoogleAppPassword),
                EnableSsl = true,
            };
            var mailMessage = new MailMessage
            {
                From = new MailAddress(From),
                Subject = Subject,
                Body = Body,
                IsBodyHtml = true,
            };
            mailMessage.To.Add(To);

            smtpClient.Send(mailMessage);
        }
    }
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449